PHP code example of sorciulus / simple-factory

1. Go to this page and download the library: Download sorciulus/simple-factory library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sorciulus / simple-factory example snippets



SimpleFactory\SimpleFactory;

class Publisher
{
    /**
     * The name of publisher
     *
     * @var string
     */
    private $name;

    /**
     * The city of publisher
     *
     * @var string|null
     */
    private $city;

    /**
     * @param string $name
     */
    public function __construct(string $name, ?string $city)
    {
        $this->name = $name;
    }

    /**
     * Get the name of publisher
     *
     * @return string
     */
    public function getName() : string
    {
        return $this->name;
    }

    /**
     * Get the city of publisher
     *
     * @return string|null
     */
    public function getCity() :?string
    {
        return $this->city;
    }
}

$factory   = new SimpleFactory(Publisher::class);
$publisher = $factory->setName('MyPublisher')->setCity('London')->make();



$otherFactory = new SimpleFactory(Publisher::class);
$newPublisher = $otherFactory->with($publisher)->make();



$otherFactory = new SimpleFactory(Publisher::class, true);
$newPublisher = $otherFactory->make();



$newPublisher = SimpleFactory::create(Publisher::class)->setName('MyPublisher')->setCity('London')->make();