class App
{
public function __construct()
{
echo "app is now running...\n";
$this->getModel('Something');
}
public function getModel($name)
{
return new $this->getModelInstance($name);
}
protected function getModelInstance($name)
{
return 'Model_' . $name;
}
}我有自动装配机和工作装置。为什么我不能以这种方式加载Model_Something,但是我得到了以下错误:
Notice: Undefined property: App::$getModelInstance in C:\Apache24\htdocs\ProjectName\app\App.php on line 13
Fatal error: Class name must be a valid object or a string in C:\Apache24\htdocs\ProjectName\app\App.php on line 13发布于 2014-07-21 21:33:33
试试这个:
public function getModel($name) {
$modelInstanceName = $this->getModelInstance($name);
return new $modelInstanceName();
}实例化类的正确方法是:
new Classname($parameter);所以当你这么做的时候
new $this->getModelInstance($name);这个班的名字是:
$this->getModelInstance所以它是在你的App中寻找getModelInstance属性(而不是函数),它并不存在。
https://stackoverflow.com/questions/24875134
复制相似问题