I've got a problem:
I'm writing a new WebApp without a Framework.
In my index.php I'm using: require_once('load.php');
And in load.php I'm using require_once('class.php');
to load my class.php.
In my class.php I've got this error:
Fatal error: Using $this when not in object context in class.php on line ... (in this example it would be 11)
An example how my class.php is written:
class foobar {
public $foo;
public function __construct() {
global $foo;
$this->foo = $foo;
}
public function foobarfunc() {
return $this->foo();
}
public function foo() {
return $this->foo;
}
}
In my index.php I'm loading maybe foobarfunc()
like this:
foobar::foobarfunc();
but can also be
$foobar = new foobar;
$foobar->foobarfunc();
Why is the error coming?
If you are invoking
foobarfunc
with resolution scope operator (::
), then you are calling it statically, e.g. on the class level instead of the instance level, thus you are using$this
when not in object context.$this
does not exist in class context.If you enable
E_STRICT
, PHP will raise a Notice about this:Do this instead
On a sidenote, I suggest not to use
global
inside your classes. If you need something from outside inside your class, pass it through the constructor. This is called Dependency Injection and it will make your code much more maintainable and less dependant on outside things.You are calling a non-static method :
Using a static-call :
When using a static-call, the function will be called (even if not declared as
static
), but, as there is no instance of an object, there is no$this
.So :
Here, the methods of your class are using the current instance of the class, as they need to access the
$foo
property of the class.This means your methods need an instance of the class -- which means they cannot be static.
This means you shouldn't use static calls : you should instanciate the class, and use the object to call the methods, like you did in your last portion of code :
For more informations, don't hesitate to read, in the PHP manual :
Also note that you probably don't need this line in your
__construct
method :Using the
global
keyword will make the$foo
variable, declared outside of all functions and classes, visibile from inside that method... And you probably don't have such a$foo
variable.To access the
$foo
class-property, you only need to use$this->foo
, like you did.Just use the Class method using this
foobar->foobarfunc();
You can not invoke method this way because it is not static method.
You should instead use:
If however you have created a static method something like:
then you can use this:
Fast method : (new foobar())->foobarfunc();
You need to load your class replace :
by :
or :
Or make static function to use
foobar::
.When you call the function in a static context,
$this
simply doesn't exist.You would have to use
this::xyz()
instead.To find out what context you're in when a function can be called both statically and in an object instance, a good approach is outlined in this question: How to tell whether I’m static or an object?