176

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?

Binit Ghetiya
  • 1,919
  • 2
  • 21
  • 31
ahmet2106
  • 4,957
  • 4
  • 27
  • 38

9 Answers9

246

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 the method this way because it is not a 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 foobarfunc() {
    return self::$foo;
}

then you can use this:

foobar::foobarfunc();
shasi kanth
  • 6,987
  • 24
  • 106
  • 158
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • 1
    The variables having the same name is not an issue. `$this->foo` is a class member, while `$foo` is just a variable in the function scope (imported from global scope). Function names with the same name as a member are also not an issue. – Gordon Feb 28 '10 at 12:08
  • 206
    You cannot use `$this` in a static method. – Yacoby Feb 28 '10 at 12:17
  • 5
    It's funny how a completely wrong answer gets upvotes nonetheless. $this is not available in class context. The OP will get the same error from the example above. – Gordon Feb 28 '10 at 12:20
  • 1
    @Ycaoby, @Gordon: I just copy-pasted his code, did not notice the `$this` keyword in there, fixed any ways thanks. – Sarfraz Feb 28 '10 at 12:23
  • 4
    @Sarfraz No offense, but it is still wrong. You *can* call an instance method with `::`. It is against `E_STRICT`, but it *does* work as long as the method body does not reference the instance scope, e.g. uses `$this`. Also, `self::foo` will not point to `$this->foo`. It references a class **constant**. Both, `self::foo` and `self::$foo` would raise a Fatal Error. – Gordon Feb 28 '10 at 12:34
  • 1
    @Gordon: he wants to return public variable set on the top. – Sarfraz Feb 28 '10 at 12:37
  • 1
    @Sarfraz Yes, I know, which is why I am complaining about your solution. The public member `foo` cannot be called from class scope, unless you either declare it static or make it into a constant. – Gordon Feb 28 '10 at 12:39
  • @Gordon: forgot that too :( Thanks – Sarfraz Feb 28 '10 at 12:45
  • @Sarfraz okay, maybe I'm too picky today, but this will still raise a fatal as self:foo => constant while self::$foo => static member. – Gordon Feb 28 '10 at 12:49
  • 4
    @Sarfraz it's better now. Sorry for pestering you, but as this became the accepted answer, I felt it necessary to point these things out :) Thanks for your patience. – Gordon Feb 28 '10 at 13:00
  • @Gordon: I highly appreciate your comment, and guess what your pestering has been really generous for me as i was missing too much. Thanks – Sarfraz Feb 28 '10 at 13:09
  • Unfortunately if `__construct` is private for whatever reason, it doesn't work, e.g.: `private function __construct()` – Firsh - justifiedgrid.com Jul 04 '15 at 11:53
34

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.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
13

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.

Gordon
  • 312,688
  • 75
  • 539
  • 559
7

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

Ramasamy Kasi
  • 171
  • 3
  • 3
5

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?

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
5

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();
    }
}
user2226755
  • 12,494
  • 5
  • 50
  • 73
2

It seems to me to be a bug in PHP. The error

'Fatal error: Uncaught Error: Using $this when not in object context in'

appears in the function using $this, but the error is that the calling function is using non-static function as a static. I.e:

Class_Name
{
    function foo()
    {
        $this->do_something(); // The error appears there.
    }
    function do_something()
    {
        ///
    }
}

While the error is here:

Class_Name::foo();
shuberman
  • 1,416
  • 6
  • 21
  • 38
0

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

Bite code
  • 578,959
  • 113
  • 301
  • 329
-1

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