0

I have seen registry pattern questions on here, but right now I'm being thrown off by the error message I'm receiving: Using $this when not in object context...

I took the code sample from http://avedo.net/101/the-registry-pattern-and-php/ and I have pored over these lines of code, but I just can't wrap my head around the problem.

EDIT: I get the feeling from the answers that the article I've linked to shouldn't be considered "working code"...

Here is a snippet of my registry class code:

class registry {

    //Holds variables, objects, etc.
    private $reg = array();

    private static $instance = null;

    public static function getInstance() {
        if($this->instance === null) {     //THROWS THE FATAL ERROR
            $this->instance = new registry();
        }
        return $this->instance;
    }

    //Disallow creation of new objects, forcing use of the Singleton
    private function __construct() {}
    private function __clone() {}

What I'm not really understanding is WHY this is throwing that error. The way I understood $this was that it referred to whatever called the method, which is this snippet from my init.php file:

    //Instantiate registry
    $registry = registry::getInstance();

I'm feeling a little burned out from reading and coding (these past few days I've been dedicated to teaching myself how MVC works by building my own barebones little web framework). I must be missing something simple, but it also seems to me that this is exactly what the article illustrates. For what it's worth, I'm using PHP 5.x.

If anyone feels they need to clear up some of these concepts for me, please feel free. :) As always, thanks for taking the time to read this.

armadadrive
  • 963
  • 2
  • 11
  • 42
  • You really should avoid using singletons and other forms of global state on your projects. – tereško Dec 03 '13 at 18:26
  • Would it be better to create a registry object and pass it to other objects that required access to its items? – armadadrive Dec 03 '13 at 18:27
  • You should probably start by watching [this](http://www.youtube.com/watch?v=-FRm3VPhseI) and [this](http://www.youtube.com/watch?v=RlfLCWKxHJ0) lecture. That should explain both the problems that are caused by global state and recommend solutions. – tereško Dec 03 '13 at 18:30
  • Great! I always love new resources for learning. Thanks @tereško – armadadrive Dec 03 '13 at 18:33
  • 1
    Then you should just start down [this list](http://stackoverflow.com/a/16356866/727208) (those two videos are in there too .. at the approximately appropriate difficulty level) – tereško Dec 03 '13 at 18:34

5 Answers5

2

You are referencing the $this context variable, which is not available when calling a method statically.

You are using the class context (calling foo::bar()) rather than the object context ($obj->foo())

As it is a singleton you should be referencing the static property using the self:: operator.

For example:

public static function getInstance() {
    if(null == self::$instance) {
        self::$instance = new self();
    }
    return self::$instance;
}
AlexP
  • 9,906
  • 1
  • 24
  • 43
1

You can't call $this inside a static method.

http://php.net/manual/en/language.oop5.static.php

skrilled
  • 5,350
  • 2
  • 26
  • 48
1

You have a static function and accessing a static var, use:

self::$instance

$this-> is used in object context, self:: is used to access static class members.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
1

It throws an error for the exact reason that it says it does.

You are not in an object context when you call a STATIC function, therefore $this does not exist.

self::$instance will work for you instead.

dockeryZ
  • 3,981
  • 1
  • 20
  • 28
1

I think you're getting confused between instance and class methods. class methods can be called without instantiating an object of that class. instance methods are called from the object, or instance, of a class. think of $this as referring to this instance of the class.

e.g.: $obj = new MyClass(); $obj->myInstanceMethod();

for class (static) methods, there is no instance, therefore, there is not this e.g.: $returnVal = MyClass::myClassMethod();

you would want to use self instead of this with class methods

let me know if you want anymore clarifications! this is one of the trickier pieces of OO to grasp at first

Jon B
  • 497
  • 1
  • 9
  • 25