PDO anonymous function inside a Factory In this question here on S.O, the accepted answer suggests to use both anonymous function and factory pattern for dealing with PDO connection. I believe the anonymous function is used in case a connection to a different database needs to be established, a different function will be defined for that. In that case, will it be alright to move the anonymous function to the factory class itself? With this approach  just passing the PDO parameters to the constructor will achieve the same as the original answer.

Something like:

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

    public function __construct( $PDO_Params )
    {
        $this-&gt;provider = function() {
            $instance = new PDO($PDO_Params["dsn"], $PDO_Params["username"], $PDO_Params["password"]);
            $instance-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $instance-&gt;setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
            return $instance;
        };
    }

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

}
TL;DR: no.

The construction that is shown in the original example provided the following functionality:


decoupling from MySQL (you dont, need disable emulation of prepared statements, if you are not using mysql, and you usually dont need to provide username and password with SQLite)
option to use MySQLi or SOAP or any other DAL without rewriting the factory (see OCP)
replacing PDO with a mock object, when running unit tests
a way to alter the anonymous function, so that you can share the same PDO instance among multiple factories (when appropriate changes added)
establishing a connection to data source, only when it is necessary (connecting to SOAP can be really slow, and RDBMS under heavy load can actually run out of "connections" that can be opened)