我的问题是这样的(在PHP):PHP从函数返回一个对象
我有三个班,
Class cls1 {
public function myfunc() {
}
}
Class cls2 {
private $_obj1; //type of cls1
...
public function getObj1() {
return $_obj1;
}
}
Class cls3 {
...
public function getMyFunc() {
$temp = new cls2();
$temp->getObj1()->myfunc(); //THIS IS NOT WORKING.
//PHP Throws an error like : Fatal error: Call to a member function getId() on a non-object in xyz.php on line 125.
}
}
我无法从CLS3经之途CLS2到达CLS1的功能。
我该如何实现它?
注意:我写了这个基本代码来显示问题。不要考虑语法错误。
“这不是工作”---请详细说明这一点。如果我们不知道真正的问题,不可能帮助您 – zerkms
您是否在cls2中的某处创建了cls1的实例?看起来你并没有创建它,只是返回null而不是预期的实例。 – zysoft
'$ temp-> getObj1()'可能返回'null'。在'$ _obj1'(cls2) – MarcDefiant