1

While trying out PHP with OOP, i came accross discussions on Stackoverflow about PDOs and not using globals and singletons. I saw this question How to properly set up a PDO connection which shows a way to use factory pattern and anonymous function for PDOs. I just had trouble understanding one part

class StructureFactory
{
    protected $provider = null;
    protected $connection = null;

    public function __construct( callable $provider )
    {
        $this->provider = $provider;
    }

    public function create( $name)
    {
        if ( $this->connection === null )
        {
            $this->connection = call_user_func( $this->provider );
        }
        return new $name( $this->connection );
    }
}

The part I didnot understand was

return new $name( $this->connection );

Is the $name a callback? Or is it an object? Why was $this->conection passed as the paramater ? Thank You in Advance

Community
  • 1
  • 1
  • `$name` should be the classname for the `new` instance. Why is this code not well documented? – JustOnUnderMillions Apr 27 '17 at 14:58
  • Maybe it will get a little clearer when you know that the variable `$this->connection` not holding a mysql connection, instead it holds an `PDO` instance. – JustOnUnderMillions Apr 27 '17 at 15:01
  • @JustOnUnderMillions `something = $factory->create('Something');` This is the way it was used. Can you explain it a bit more? I couln't understand how i can use the pdo inside another class method –  Apr 27 '17 at 15:01

1 Answers1

0
class Something {
   protected $PDO;
   public function __construct(\PDO $PDO){
      //after this, you can use the PDO instance in your class.
      $this->PDO = $PDO;
   }
}

//get an class instance that holds an refer to the PDO class
$something = $factory->create('Something');
  • As you can see in the link_answer in you post $provider returns an PDO instance.
  • If you know use $factory->create('Something'); you get an class instance of Something where the PDO instance from StructureFactory is injected.

But in my opinion StructureFactory::create() is kinda useless. What if the given class has more constructor parameter? How do we set them with create()?

Normaly a getInstance() is enuff

public function getInstance()
{
    if ( $this->connection === null )
    {
        $this->connection = call_user_func( $this->provider );
    }
    return $this->connection;
}

I dont have read the link fully but with my version you can do outside the factory this

 $something = new Something($factory->getInstance(),$anotherParameter);
JustOnUnderMillions
  • 3,741
  • 9
  • 12
  • Oh, so `$something` can be `$anything = $factory->create('something');`? –  Apr 27 '17 at 15:08
  • @feetnappy You can call/name variables like you want. But `'something'` must be an declared class if you use `create()`. Test it with just `$factory->create('NonExistingClassName');` will give an `Fatal Error` – JustOnUnderMillions Apr 27 '17 at 15:10
  • I see, now I understand this much better. That question was targetted towards more experienced programmers, and not for absolute starters like us, so thanks for answering simple questions like this. –  Apr 27 '17 at 15:14