我有一个带有构造函数的简单查询方法:
class mysql_a {
public $db;
public function __construct($db) {
$this->db=$db;
}
public function find_by_id() {
$result = $this->db->query('SELECT * FROM users WHERE id=2 LIMIT 1')->fetch(PDO::FETCH_ASSOC);
return $result;
}
}
$found = new mysql_a($db);
$res = $found->find_by_id();
echo $res['username'];
如何使函数“find_by_id”静态?我的意思是这样的:
public static function find_by_id() {
$result = self::$db->query('SELECT * FROM users WHERE id=2 LIMIT 1')->fetch(PDO::FETCH_ASSOC);
return $result;
}
以及如何执行它?我的意思是如何改变这部分:
$found = new mysql_a($db);
$res = $found->find_by_id();
echo $res['username'];
我想不通,它让我发疯......有什么想法吗?谢谢,顾。