96

I have this method that I want to use $this in but all I get is: Fatal error: Using $this when not in object context.

How can I get this to work?

public static function userNameAvailibility()
{
     $result = $this->getsomthin();
}
0

8 Answers 8

169

This is the correct way

public static function userNameAvailibility()
{
     $result = self::getsomthin();
}

Use self:: instead of $this-> for static methods.

See: PHP Static Methods Tutorial for more info :)

4
  • 20
    You should also remember, that the getsomthin() method has to be static too - You can't call non-static inside a static method.
    – thorinkor
    Jul 9, 2013 at 10:10
  • 7
    @Sarfraz, shouldn't it be static:: instead of self::?
    – Pacerier
    Jul 30, 2013 at 10:18
  • Is there any way to do something similar to this in C/C++? Sep 11, 2013 at 15:28
  • how about the variable Jun 13, 2022 at 3:47
15

You can't use $this inside a static function, because static functions are independent of any instantiated object. Try making the function not static.

Edit: By definition, static methods can be called without any instantiated object, and thus there is no meaningful use of $this inside a static method.

2
  • 3
    There should be when you are trying to assign the static variable to an instance variable. Is this not possibel?
    – Jom
    Feb 18, 2010 at 6:27
  • 1
    self::staticMethod() or $self = new self(); and $self->nonStaticMethod();
    – xavier bs
    Dec 6, 2020 at 15:51
6

Only static functions can be called within the static function using self:: if your class contains non static function which you want to use then you can declare the instance of the same class and use it.

<?php
class some_class{
function nonStatic() {
    //.....  Some code ....   
    }
 Static function isStatic(){
    $someClassObject = new some_class;
    $someClassObject->nonStatic();
    } 
}
?>
3

The accessor this refers to the current instance of the class. As static methods does not run off the instance, using this is barred. So one need to call the method directly here. The static method can not access anything in the scope of the instance, but access everything in the class scope outside instance scope.

0
1

It's a pity PHP doesn't show a descriptive enough error. You can not use $this-> inside a static function, but rather use self:: if you have to call a function inside the same class

1

Here is an example of what happens when a method of a class is called in a wrong way. You will see some warnings when execute this code but it will work and will print: "I'm A: printing B property". (Executed in php5.6)

class A {
    public function aMethod() {
        echo "I'm A: ";
        echo "printing " . $this->property;
    }
}

class B {
    public $property = "B property";

    public function bMethod() {
        A::aMethod();
    }
}

$b = new B();
$b->bMethod();

It seams that the variable $this, used in a method which is called as a static method, points to the instance of the "caller" class. In the example above there is $this->property used in the A class which points to a property of the B.

EDIT:

The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object). PHP > The Basics

1

In the static method,properties are for the class, not the object. This is why access to static methods or features is possible without creating an object. $this refers to an object made of a class, but $self only refers to the same class.

0

Try return (new static)->bar($string)

protected static function foo($string)
{
     // $bar = $this->createHashFromString(string); //no
     $bar = self::staticHash($string);
     // ...
}

// give it a different name otherwise you're in loop. 
protected static function staticHash($string): string
{
    return (new static)->createHashFromString($string);
}

// this might be in a trait for a hash function you're using sitewide
// and you can access it a static function via the above function
function createHashFromString($string): string
{
    return hash('xxh128', $string);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.