Goodmorning everyone。 我編寫返回我的類:PHP擴展類
Notice: Undefined variable: db in /Applications/MAMP/htdocs/Test Vari/index.php on line 12
Fatal error: Call to a member function row() on null in /Applications/MAMP/htdocs/Test Vari/index.php on line 12
這是php文件的第一部分:
session_start();
$_SESSION['ID'] = 1;
require_once 'pass/password.inc.php'; /*Here's the DB Class*/
我擴展一個類到另一個,這裏是代碼:
class pg extends DB{
public $id;
function __construct(){
parent::__construct();
$this->id = $_SESSION['ID'];
}
public function pgname(){
$rs = $db->row("SELECT CONCAT(Nome, ' ', Cognome) FROM Personaggio WHERE id = :id",array("id"=>$this->id));
return($rs);
}
}
$pg = new pg();
print_r ($pg->pgname());
$ db-> row()是在我擴展的DB類中聲明的,並且我確定這是工作的。 DB類是沒有初始化,當我這樣做,錯誤是一樣的,這是我要做的事:
class pg extends DB{
public $id;
public $db;
function __construct(){
parent::__construct();
$this->db = new DB();
$this->id = $_SESSION['ID'];
}
public function pgname(){
$rs = $db->row("SELECT CONCAT(Nome, ' ', Cognome) FROM Personaggio WHERE id = :id",array("id"=>$this->id));
return($rs);
}
}
,當我在print_r($pg->pgname);
是的,像這樣: <?php session_start(); $ _SESSION ['ID'] = 1; require_once'pass/password.inc.php'; require_once'pass/password.inc.php'; –
public function pgname($ id){ $ rs = $ this-> db-> row(「SELECT CONCAT(Nome,',Cognome)FROM Personaggio WHERE ID =:id」,array(「id」=> $ ID)); return($ rs); } $ pg = new pg(); echo($ pg-> pgname($ _ SESSION ['ID'])); 仍然不工作,這是輸出: 注意:未定義的屬性:pg :: $ db在/ Applications/MAMP/htdocs /測試版第9行上的Vari/index.php 致命錯誤:致電成員函數row()on null在/ Applications/MAMP/htdocs/Test Vari/index.php on line 9 –
閱讀我的回答'$ rs = $ this-> row(「....'not'$ this-> db - > row('你不需要'db',因爲你已經擴展了DB類,所有的類都被看作是一個單獨的實體 – RiggsFolly