我遇到了「調用非對象成員函數」的問題 - 錯誤。 字段$ other_class不適用於將來的操作。 如何填寫和使用對象$ other_class?謝謝。對象在自己的類中不可見PHP 5 OOP
$myclass = new MyClass;
$other_class = $myclass -> GetOther_Class();
var_dump($other_class); //Works!
echo $other_class; //Call to a member function on a non-object - Error
class MyClass
{
private $other_class;
function __construct()
{
$other_class = new Other_Class; //Fill $other_class
//I tried also
//$this -> other_class = new Other_Class;
}
public function GetOther_Class()
{
return $other_class;
}
private function Generate()
{
$other_class -> SetTitle ("Hello");
}
public function __toString()
{
$this->Generate();
}
}
總是使用'$ this->'來訪問類成員(除非它們是靜態的)。 – x4rf41
錯誤不在你說的行中。當你修好後你會得到下一個。所以要小心:爲了正確理解錯誤,您需要找到實際產生的代碼行。 – hakre