我有一个为工厂方法编写的类,并且在第 22 行不断收到错误消息,其中指出:
致命错误:不在对象上下文中使用 $this
我看过其他人的帖子和类似的问题,但可惜我不明白发生了什么,无法将他们作为答案应用到我的情况。
我的班级是这样称呼的:
$class = AisisCore_Factory_Pattern('class_you_want');
然后从那里执行以下操作:
class AisisCore_Factory_Pattern {
protected static $_class_instance;
protected static $_dependencies;
public function get_instance(){
if(self::$_class_instance == null){
$_class_instance = new self();
}
return self::$_class_instance;
}
public function create($class){
if(empty($class)){
throw new AisisCore_Exceptions_Exception('Class cannot be empty.');
}
if(null === self::$_dependencies){
$this->_create_dependecies();
}
if(!isset(self::$_dependencies['dependencies'][$class])){
throw new AisisCore_Exceptions_Exception('This class does not exist in the function.php dependecies array!');
}
if(isset(self::$_dependencies['dependencies'][$class]['arguments'])){
$new_class = new $class(implode(', ', self::$_dependencies['dependencies'][$class]['params']));
return $new_class;
}else{
$new_class = new $class();
return $new_class;
}
}
private function _create_dependecies(){
self::$_dependencies = get_template_directory() . '/functions.php';
}
}
它吓坏了:
$this->_create_dependecies();
我不确定这是如何脱离上下文或如何正确称呼它....