PHP Fatal error: Using $this when not in object context

I’ve got a problem:

I’m writing a new WebApp without a Framework.

In my index.php im 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?

getting Fatal error: Using $this when not in object context in Stemmer.php on line 317 [duplicate]

This question already has an answer here: PHP Fatal error: Using $this when not in object context 6 answers I am getting a Fatal error: Using $this when not in object context in Stemmer.php on

Fatal error: Using $this when not in object context explanation?

I’m getting this strange error which I have never got before. Fatal error: Using $this when not in object context Chat.php (class) <?php class Chat { private $_data = array(), $_db; public funct

{PHP} Fatal error: Using $this when not in object context (Joomla)

i don’t really know php and have hit a brick wall. The problem is that my site is showing the following error… Fatal error: Using $this when not in object context in /hermes/web07/b2350/pow.thefoodi

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

This question already has an answer here: PHP Fatal error: Using $this when not in object context 6 answers i am using below code, suddenly its start throwing an error like Fatal error: Using $

Fatal error: Using $this when not in object context

here is the part if having error. Fatal error: Using $this when not in object context in /pb_events.php on line 6 line 6 is: $jpp = $this->vars->data[jpp]; function DoEvents($this) { global

Fatal Error: Using $this when not in object context

I’m receiving this fatal error message: Using $this when not in object context. This class is setted up as a library in the CodeIgniter. This is my class: class My_class { function __construct() { $th

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

This question already has an answer here: Fatal error: Using $this when not in object context 4 answers i have this class for connect to mysql database using php/mysqli: class AuthDB { private

“Fatal error: Using $this when not in object context” but code IS in object context

I’m getting the old familiar Fatal error: Using $this when not in object context referring to $this->test = ‘test’; in the following class: class Example { public $test; public function index() {

PHP Error: Using $this when not in object context

I keep getting this error PHP Fatal error: Using $this when not in object context and I cannot for the life of me figure out why. Is there another set of eyes that can help? I have another class set u

I am using $this in a class, but it keeps giving me Fatal error: Using $this when not in object context [duplicate]

This question already has an answer here: PHP Fatal error: Using $this when not in object context 6 answers I created a a cart class which contains songs to be bought. The cartSong class works

Answers

In my index.php I’m loading maybe foobarfunc() like this:

 foobar::foobarfunc();  // Wrong, it is not static method

but can also be

$foobar = new foobar;  // correct
$foobar->foobarfunc();

You can not invoke method this way because it is not static method.

foobar::foobarfunc();

You should instead use:

foobar->foobarfunc();

If however you have created a static method something like:

static $foo; // your top variable set as static

public static function foo() {
    return self::$foo;
}

then you can use this:

foobar::foobarfunc();

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:

Strict Standards: 
Non-static method foobar::foobarfunc() should not be called statically

Do this instead

$fb = new foobar;
echo $fb->foobarfunc();

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.

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?

You are calling a non-static method :

public function foobarfunc() {
    return $this->foo();
}

Using a static-call :

foobar::foobarfunc();

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 :

  • You should not use static calls for non-static methods
  • Your static methods (or statically-called methods) can’t use $this, which normally points to the current instance of the class, as there is no class instance when you’re using static-calls.

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 :

$foobar = new foobar();
$foobar->foobarfunc();

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 :

global $foo;

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.

$foobar = new foobar; put the class foobar in $foobar, not the object. To get the object, you need to add parenthesis: $foobar = new foobar();

Your error is simply that you call a method on a class, so there is no $this since $this only exists in objects.

First you understand one thing, $this inside a class denotes the current object.
That is which is you are created out side of the class to call class function or variable.

So when you are calling your class function like foobar::foobarfunc(), object is not created. But inside that function you written return $this->foo(). Now here $this is nothing. Thats why its saying Using $this when not in object context in class.php

Solutions:

  1. Create a object and call foobarfunc().

  2. Call foo() using class name inside the foobarfunc().

Fatal error: Call to undefined method wpdb::esc_like() in /home/abobbysp/public_html/wp-content/plugins/woocommerce/includes/class-wc-install.php on line 142

when i try to activate this plugin i got this error. Please help

Just use the Class method using this foobar->foobarfunc();

Fast method : (new foobar())->foobarfunc();

You need to load your class replace :

foobar::foobarfunc();

by :

(new foobar())->foobarfunc();

or :

$Foobar = new foobar();
$Foobar->foobarfunc();

Or make static function to use foobar::.

class foobar {
    //...

    static function foobarfunc() {
        return $this->foo();
    }
}