Một đơn giản để thực hiện phương pháp để giúp bạn bắt đầu có thể đi như:
class db
{
public function __call($function, $arguments)
{
switch($function) {
// implement table handling here
case 'user':
//do something
return $something;
break;
}
}
}
Tùy thuộc vào hay không, bạn muốn đi phức tạp, nhưng rắn hoặc đơn giản, nhưng ít linh hoạt bạn có thể thực hiện hai chiến lược khác nhau. chiến lược đơn giản có thể đi như vậy:
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;
}
}
}
Cách khác, nhưng mạnh mẽ hơn:
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;
}
}
Bạn nhận được các ý tưởng. Hoặc là quá tải đối tượng db hoặc tạo đối tượng db cơ sở và đối tượng bảng, đặt nhiều thông minh vào đối tượng bảng, đảm bảo rằng khi được tạo, đối tượng bảng lưu trữ tham chiếu đến đối tượng db
Dude this is ** chaining **, không làm tổ. – Pacerier