-1
我試圖訪問另一個擴展類中的類的對象。PHP訪問另一個類中某個類的對象
class MainClass{
protected $theobject;
function __construct(){
$this->theobject = new AnotherClass();
}
}
class TheClass extends MainClass{
function AnotherFunction(){
$this->theobject->SomeFunction();
}
}
我在$this->theobject->AnotherFunction()
上遇到錯誤。錯誤是「調用一個非對象的成員函數SomeFunction()」。
但是這工作得很好:
class TheClass{
protected $theobject;
function SoemFunction(){
$this->theobject = new AnotherClass();
$this->theobject->SomeFunction();
}
}
它甚至法律要做到這一點在PHP?
幾乎所有代碼:http://3v4l.org/MaXe6
當我的var_dump的$這個 - >在theClass描述theobject是回來爲NULL。
你能否提供類AnotherClass的聲明?如果您確定它具有該功能,那麼這樣做完全有效。 –
AnotherClass中的所有內容都設置爲公開。 – Ameer
請了解依賴注入 –