0

I have this class which I wrote for a factory method and I keep getting an error on line 22 which states:

Fatal error: Using $this when not in object context

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.

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....

3
  • 1
    If you're going to call your functions statically, define them as static, and use self::_create_dependencies() Commented Dec 14, 2012 at 23:38
  • How are you calling create() on the class? Commented Dec 14, 2012 at 23:39
  • Why is this a singleton? Commented Dec 14, 2012 at 23:45

3 Answers 3

0

So if you are trying to do this as a singleton your get_instance() method needs to be defined statically. further how are you calling create()? I see no reference to that at all. Perhaps create() should be a public static function and then change your $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. 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

Sign up to request clarification or add additional context in comments.

Comments

0

You error says you are using $this when you can't, so here is the part:

$this->_create_dependecies();

change it to :

self::_create_dependecies();

3 Comments

Isn't using self:: meant for static functions? Correct me if I am wrong, not sure.
It is, but that's what happens when you use singletons: madness. Madness and confusion.
yes it is, but you can't use $this in your context because you didn't instantiate the object
-1

I believe you should be writing $class = new AisisCore_Factory_Pattern("class_you_Want");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.