Una semplice da implementare il metodo per iniziare potrebbe andare come:
class db
{
public function __call($function, $arguments)
{
switch($function) {
// implement table handling here
case 'user':
//do something
return $something;
break;
}
}
}
A seconda se si desidera o meno andare complicato, ma solido o semplice, ma meno flessibile si potrebbe implementare due diverse strategie. semplice strategia potrebbe andare in questo modo:
class db
{
protected $operatingTable;
public function limit($limitNumber)
{
return $this->executeQuery("SELECT * FROM ".$this->operatingTable." LIMIT ".$limitNumber); // where executeQuery is a function that runs a query
}
public function __call($function, $arguments)
{
switch($function) {
// implement table handling here
case 'user':
$this->operatingTable='user'; // alternately, but less secure: $this->operatingTable=$function;
return $this;
break;
}
}
}
In alternativa, ma più potente:
class db
{
protected $operatingTable;
public function limit($limitNumber)
{
return $this->executeQuery("SELECT * FROM ".$this->operatingTable." LIMIT ".$limitNumber); // where executeQuery is a function that runs a query
}
public function __call($function, $arguments)
{
switch($function) {
// implement table handling here
case 'user':
$user = new user($this); // pass in the database to the object, so the table object can have a reference to the db
return $user;
break;
}
}
}
class baseTableClass
{
protected $db; // an instance of class db
function limit($limitNumber)
{
$db->execute($aStatementDerivedFromThisClassesInformation); // execute a sql command, based on information about the table in the class
}
}
class user extends baseTableClass
{
public function __construct($db) {
$this->db = $db;
}
}
Si ottiene l'idea. O sovraccarichi l'oggetto db, o crei un oggetto db di base, e oggetti tabella, mettendo molta intelligenza negli oggetti tabella, assicurando che quando viene creato, un oggetto tabella memorizzi un riferimento all'oggetto db
Amico, questo è ** concatenamento **, non annidato. – Pacerier