简体   繁体   English

致命错误:不在对象上下文中时使用$ this

[英]Fatal error: Using $this when not in object context

I have this class which I wrote for a factory method and I keep getting an error on line 22 which states: 我有我为工厂方法编写的此类,并且在第22行不断收到错误,其中指出:

Fatal error: Using $this when not in object context 致命错误:不在对象上下文中时使用$ this

I have seen other peoples posts and similar questions but alas I don't understand whats going on enough to be able to apply what they took as an answer to my situation. 我见过其他人的帖子和类似的问题,但是,I,我不了解发生了什么事情,以致无法将他们所采取的措施应用于我的情况。

my class is called as such: 我的班级是这样称呼的:

$class = AisisCore_Factory_Pattern('class_you_want');

And then from there the following is executed: 然后从那里执行以下操作:

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';
    }

}

where it freaks out is on: 吓坏了的地方是:

$this->_create_dependecies();

I'm not sure how that's out of context or how I would properly call it.... 我不确定这是如何脱离上下文的,或者我怎么称呼它...。

You error says you are using $this when you can't, so here is the part: 您的错误是您无法使用$this时,这里是其中的一部分:

$this->_create_dependecies();

change it to : 更改为:

self::_create_dependecies();

So if you are trying to do this as a singleton your get_instance() method needs to be defined statically. 因此,如果您尝试以单例方式进行此操作,则需要静态定义get_instance()方法。 further how are you calling create()? 进一步,您如何调用create()? I see no reference to that at all. 我完全没有提及。 Perhaps create() should be a public static function and then change your $this->_create_dependecies(); 也许create()应该是一个公共静态函数,然后更改$ this-> _ create_dependecies(); to self::_create_dependencies() and add the static keyword to your definition of the create_dependencies method so it would be public static function create_dependencies. 到self :: _ create_dependencies()并将static关键字添加到create_dependencies方法的定义中,这样它将是公共静态函数create_dependencies。 Then you put it all together with... 然后,您将所有内容与...组合在一起

$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';
}

} }

and that should do ya. 那应该做的。 if you really want to access it non statically (yes I know that's not a real word but I like it) you should be saying something like this... 如果您真的想非静态地访问它(是的,我知道这不是一个真实的词,但我喜欢它),您应该说这样的话...

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

of course in the second example you would remove the static keyword from the create function definition 当然,在第二个示例中,您将从create函数定义中删除static关键字

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM