我有這樣的麻煩:在模型類中我創建了一個受保護的變量,但是在Model類的子類中,他不可用。PHP:未定義的子變量
class Model{
protected $db = null;
public function __construct(){
require_once 'app/classes/db.php';
$db = new DB();
}
}
這是他們的孩子:
class Model_Main extends Model{
public function get_data(){
$db->select('news');
}
}
錯誤:
Notice: Undefined variable: db in /var/www/localhost/htdocs/app/models/model_main.php on line 5
Fatal error: Call to a member function select() on null in /var/www/localhost/htdocs/app/models/model_main.php on line 5
你可能後'$這個 - > db' – meagar
第一次使用PHP OOP?只需閱讀一次文檔:http://php.net/manual/en/language.oop5.basic.php – sectus