0

我有一个为工厂方法编写的类,并且在第 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();

我不确定这是如何脱离上下文或如何正确称呼它....

4

3 回答 3

0

你的错误说你在$this不能使用的时候使用,所以这里是部分:

$this->_create_dependecies();

将其更改为:

self::_create_dependecies();
于 2012-12-14T23:38:56.963 回答
0

因此,如果您尝试以单例方式执行此操作,则需要静态定义 get_instance() 方法。进一步你如何调用create()?我根本没有看到任何提及。也许 create() 应该是一个公共静态函数,然后改变你的 $this->_create_dependecies(); 到 self::_create_dependencies() 并将 static 关键字添加到 create_dependencies 方法的定义中,使其成为公共静态函数 create_dependencies。然后你把它们放在一起......

$class = AisisCore_Factory_Pattern::create('class_you_want');

class AisisCore_Factory_Pattern {

  protected static $_class_instance;

  protected static $_dependencies;

  public static function get_instance(){
    if(self::$_class_instance == null){
        $_class_instance = new self();
    }

    return self::$_class_instance;
  }

  public static function create($class){
    if(empty($class)){
        throw new AisisCore_Exceptions_Exception('Class cannot be empty.');
    }

    if(null === self::$_dependencies){
        self::_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 static function _create_dependecies(){
    self::$_dependencies = get_template_directory() . '/functions.php';
}

}

那应该可以。如果你真的想非静态地访问它(是的,我知道这不是一个真实的词,但我喜欢它)你应该说这样的话......

$class = AisisCore_Factory_Pattern::get_instance()->create('class_you_want');
//or...
$class = AisisCore_Factory_Pattern::get_instance();
$newclass = $class->create('class_you_want');

当然,在第二个示例中,您将从 create 函数定义中删除 static 关键字

于 2012-12-15T00:22:24.047 回答
-1

我相信你应该写$class = new AisisCore_Factory_Pattern("class_you_Want");

于 2012-12-14T23:38:30.123 回答