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

后端 未结 9 2244
日久生厌
日久生厌 2020-11-22 03:48

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\');

相关标签:
9条回答
  • 2020-11-22 04:20

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

    0 讨论(0)
  • 2020-11-22 04:26

    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 :

    • The Classes and Objects section
    • And the Static Keyword page.


    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.

    0 讨论(0)
  • 2020-11-22 04:29

    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();
    
    0 讨论(0)
提交回复
热议问题