-1

i'm getting the "Uncaught Error: Using $this when not in object context" error on this code

    public static function instance($settings)
    {
        if (self::$instance == NULL) {
            self::$instance = new $this($settings);
        }
        return self::$instance;
    }
}

frankly i don't know much about php so i would appreciate some help.

thanks in advance

  • Can you please update your code to adhere to the guidelines on creating a [mre]? – esqew May 31 '22 at 20:56
  • There's no `$this` in a static method. – Barmar May 31 '22 at 20:59
  • You can google the error message easily enough. But basically a "static" method doesn't relate to a specific instance of a class/object, therefore $this (which represents the current instance of a class/object) cannot be used within it, because it's out of context. You'll need to learn some of the key concepts about object oriented programming to get your head fully round this – ADyson May 31 '22 at 21:00
  • And it's not clear what you intend with `new $this($settings)`. `new` has to be followed by a class name, but `$this` is a class instance. – Barmar May 31 '22 at 21:00
  • To make a new instance of the same class, use `new self`, not `new $this`. – Barmar May 31 '22 at 21:01
  • @Barmar thanks for the reply, frankly i haven't wrote the code as i said before i don't know much about php. but i checked the full file and i think different settings is being saved on $this then on this code which i sent ,he checks the $instance and if its empty hes loading $this on it... i think its something like that. i would appreciate if you could help me out on this – Hadi Heidary May 31 '22 at 22:08
  • Just change `new $this` to `new self` and it should just work. There's nothing here that depends on the properties of a specific element. – Barmar May 31 '22 at 22:10
  • Did you look at the linked question? It shows exactly how to do what you're trying to do. – Barmar May 31 '22 at 22:11
  • @Barmar when i change it to "self::$instance = new self($settings);" i'm getting 503 server error which i have no idea why... – Hadi Heidary May 31 '22 at 22:20
  • @Barmar to add more details this php file works ok when encrypted with ioncube but when i put the raw code it gives me this error – Hadi Heidary May 31 '22 at 22:22
  • When you get a 5xx error, check the PHP error log for the specific reason. – Barmar May 31 '22 at 22:25
  • @Barmar i don't have any error log about 503 error on my error log only the errors regarding using $this when not in object mode etc.... do you know why the code works when encrypted with ioncube but gives error when using the code itself? seems weird to me. and as i said before i thinks $this is for loading the different settings that existed on the php file to $instance , its being used many times regarding the theme settings. it would be nice if you could look at the whole php file if you have the time. – Hadi Heidary May 31 '22 at 22:48
  • The error log won't show 503, that's something the webserver adds when PHP gets a fatal error. You just need to find the PHP fatal error message. See https://stackoverflow.com/questions/2687730/how-can-i-make-php-display-the-error-instead-of-giving-me-500-internal-server-er – Barmar May 31 '22 at 22:49
  • I'll just repeat what everyone else has said: there's no `$this` in a static method. `$this` is only set when you call a method through an object, like `$variable->instance($settings)`. Then `$this` will contain the object that was in `$variable`. – Barmar May 31 '22 at 22:51
  • @Barmar ok so to enable error reporting apparently some people said server need to be restarted which i can't since im on a shared hosting i tried to changed php version since some people said it would restart it but website didn't show any error after doing so.... one thing that i realy want to understand here is why this code works when encrypted with ioncube but doesn't work when i use the code itself. this is my main question right now... i mean how is this possible? since ioncube loader only translate the code for php and nothing more... – Hadi Heidary May 31 '22 at 23:24

0 Answers0