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 classThis is the factory class with the different CRUD-operations and the queries for the databaseclass OrganisationFactory{    public function getOrganisations()    {        $r = [];        $q = new MysqlQuery(SELECT ID, Name, Created FROM organisations;, [0], MysqlConnect::get('core'));        $q->select('', []);        foreach($q->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->select('i', [$ID]);        foreach($q->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->insert('ss', [$obj->getName(), $obj->getCreated()]);        $obj->setID($q->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->update('ssi', [$obj->getName(), $obj->getCreated(), $obj->getID()]);        return $obj;    }    public function deleteOrganisation($obj)    {        /* @var $obj Organisation */        $q = new mysqlQuery('DELETE FROM organisations WHERE ID = ?;', [0], mysqlConnect::get('core'));        $q->delete('i', [$obj->getID()]);        return $obj;    }}Base ClassThis is the base class of the object including the variables, constructor and the getters and settersclass Organisation {    //variablen    private $_ID = 0;    private $_Name = '';    private $_Created = '';    //constructor    function __construct($ID, $Name, $Created) {        $this->_ID = $ID;        $this->_Name = $Name;        $this->_Created = $Created;    }    //getter & setters    public function getID() { return $this->_ID; }    public function setID($ID) { $this->_ID = $ID; }    public function getName() { return $this->_Name; }    public function setName($Name) { $this->_Name = $Name; }    public function getCreated() { return $this->_Created; }    public function setCreated($Created) { $this->_Created = $Created; }}
Correct implementation of the factory pattern in php
php;factory method
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 classesrather than by calling a constructor (4).(emphasis, mine)The key points here are:This is a creational pattern (it creates objects) - Your code satisfies thisObjects 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 objectCode using the factory doesn't call a constructor directlyYour 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:BusinessNonProfitOrganisationSample 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->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.