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