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->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 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->_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; }
}