Correct implementation of the factory pattern in php All, during my vacation I am trying to learn myself the basic principles of OOP and the factory pattern in php. I read a lot of tutorials and websites (e.g. http://www.phptherightway.com) and began to write some classes.

Is the underlying implementation correct?

Additional information:
I have set up a singleton class for my MysqlConnection. Furthermore I created a MysqlQuery class to handle the queries to the database. 

Thanks for the advice!

Factory class

This is the factory class with the different CRUD-operations and the queries for the database

class OrganisationFactory
{
    public function getOrganisations()
    {
        $r = [];
        $q = new MysqlQuery("SELECT ID, Name, Created FROM organisations;", [0], MysqlConnect::get('core'));
        $q-&gt;select('', []);
        foreach($q-&gt;getResult() as $r)
        {
            $r[] = new Organisation($q['ID'], $q['Name'], $q['Created']);
        }
        return $r;
    }

    public function getOrganisation($ID)
    {
        $r = 0;
        $q = new MysqlQuery("SELECT ID, Name, Created FROM organisations WHERE ID = ?;", [0], MysqlConnect::get('core'));
        $q-&gt;select('i', [$ID]);
        foreach($q-&gt;getResult() as $r)
        {
            $r = new Organisation($q['ID'], $q['Name'], $q['Created']);
        }
        return $r;
    }

    public function createOrganisation($obj)
    {
        /* @var $obj Organisation */
        $q = new mysqlQuery('INSERT INTO organisations (Name, Created) VALUES (?, ?);', [0], mysqlConnect::get('core'));
        $q-&gt;insert('ss', [$obj-&gt;getName(), $obj-&gt;getCreated()]);
        $obj-&gt;setID($q-&gt;getResult());
        return $obj;
    }

    public function updateOrganisation($obj)
    {
        /* @var $obj Organisation */
        $q = new mysqlQuery('UPDATE organisations SET Name = ?, Created = ? WHERE ID = ?;', [0], mysqlConnect::get('core'));
        $q-&gt;update('ssi', [$obj-&gt;getName(), $obj-&gt;getCreated(), $obj-&gt;getID()]);
        return $obj;
    }

    public function deleteOrganisation($obj)
    {
        /* @var $obj Organisation */
        $q = new mysqlQuery('DELETE FROM organisations WHERE ID = ?;', [0], mysqlConnect::get('core'));
        $q-&gt;delete('i', [$obj-&gt;getID()]);
        return $obj;
    }
}


Base Class

This is the base class of the object including the variables, constructor and the getters and setters

class Organisation 
{
    //variablen
    private $_ID = 0;
    private $_Name = '';
    private $_Created = '';

    //constructor
    function __construct($ID, $Name, $Created) {
        $this-&gt;_ID = $ID;
        $this-&gt;_Name = $Name;
        $this-&gt;_Created = $Created;
    }

    //getter &amp; setters
    public function getID() { return $this-&gt;_ID; }
    public function setID($ID) { $this-&gt;_ID = $ID; }

    public function getName() { return $this-&gt;_Name; }
    public function setName($Name) { $this-&gt;_Name = $Name; }

    public function getCreated() { return $this-&gt;_Created; }
    public function setCreated($Created) { $this-&gt;_Created = $Created; }
}
No, this is not 100% true to the Factory Pattern - almost, but not 100%.

From Wikipedia:


  In class-based programming, the factory method pattern is a creational pattern (1) that uses factory methods to deal with the problem of creating objects without having to specify the exact class (2) of the object that will be created. This is done by creating objects by calling a factory method (3)—either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling a constructor (4).


(emphasis, mine)

The key points here are:


This is a creational pattern (it creates objects) - Your code satisfies this
Objects get created without having to specify the exact class. The factory pattern deals with inheritance - You are specifying an exact class, and only have one class.
A method is used to create the object
Code using the "factory" doesn't call a constructor directly


Your code satisfies points 3 and 4, with one fatal problem: The createOrganization method also INSERTs a record into the database. If a factory method is written correctly, the only thing it does is create an object. A factory method or class should never do anything else.

The major reason why you haven't actually created a "factory" is because you do not utilize inheritance, where you create a concrete instance and return it as an abstract parent class or interface.

Instead, what you have created is a Data Access object.

Now, a data access object can utilize a factory. Let's pretend your Organisation class is abstract, and it has two concrete child classes:


Business
NonProfitOrganisation


Sample code:

class OrganisationDataAccess
{
    private function createOrganisation($data) : Organisation {
        if ($data['type'] == 'business') {
            return new Business($data['ID'], $data['Name'], $data['Created']);
        } else if ($data['type'] == 'non-profit') {
            return new NonProfitOrganisation($data['ID'], $data['Name'], $data['Created']);
        }

        throw new Exception('Invalid organisation type: ' + $data['type']);
    }

    public function getOrganisation($id) {
        $data = // get from database

        $org = $this-&gt;createOrganisation($data);

        // Populate $org with more data

        return $org;
    }
}


Now the createOrganisation class is a true factory method. It instantiates concrete child classes and returns them as the abstract parent.

Now, simply because you aren't 100% true to the definition doesn't mean your createOrganisation method isn't useful - it eliminates duplication of code when creating new objects. But I would suggest a name change for your class: OrganisationDataAccess or OrganisationRepository is a better fit.

If, in the future, you really do need sub classes you have the option of adding an OrganisationFactory class if need be.