What is difference between self and static in php?

When you use self to refer to a class member, you're referring to the class within which you use the keyword. In this case, your Foo class defines a protected static property called $bar. When you use self in the Foo class to refer to the property, you're referencing the same class.

Therefore if you tried to use self::$bar elsewhere in your Foo class but you had a Bar class with a different value for the property, it would use Foo::$bar instead of Bar::$bar, which may not be what you intend:

class Foo
{
    protected static $bar = 1234;
}

class Bar extends Foo
{
    protected static $bar = 4321;
}

When you call a method via static, you're invoking a feature called late static bindings [introduced in PHP 5.3].

In the above scenario, using self will result in Foo::$bar[1234]. And using static will result in Bar::$bar [4321] because with static, the interpreter takes into account the redeclaration within the Bar class during runtime.

// self
var_dump[Foo::$bar];
// [int] 1234

// static
var_dump[Bar::$bar];
// [int] 4321

You typically use late static bindings for methods or even the class itself, rather than properties, as you don't often redeclare properties in subclasses; an example of using the static keyword for invoking a late-bound constructor can be found in this related question: New self vs. new static

However, that doesn't preclude using static with properties as well.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    New self: The self is keyword in PHP. It refers to the same class in which the new keyword is actually written. It refers to the class members, but not for any particular object. This is because the static members[variables or functions] are class members shared by all the objects of the class. The function called self::theFunction[] behaves like “I will execute in the context of the class whom I physically belong to.”[Assuming the inheritance scenario].

    • Example: Suppose we make this call to the static model function in the Car class – since it is a static function we can, of course, call the function directly using only the class name:

    • Output:
      I am a Car!
      I am a Car!
    • Explanation: The model function is defined inside the Car class, and it is not overridden by the Mercedes class– but the model function is of course inherited by the Mercedes class.
      As a result, when we call the version of the model inside the Mercedes class, the scope of the function is still inside the Car class– because the function definition is inside the Car class. The way the keyword “self” works are that it will call the current class’s implementation of the getModel function – and since the model function is defined inside the Car class, the current class would be the Car class.

      So, it will call the Car class implementation of getModel and NOT the Mercedes class implementation. This behavior may be considered undesirable because it is not polymorphic, and is not aligned with the object-oriented design principles. But, there is an alternative solution that can get us that kind of behavior– and this is where the static keyword becomes useful.

    New static: The static is a keyword in PHP. Static in PHP 5.3’s late static bindings, refers to whatever class in the hierarchy you called the method on. The most common usage of static is for defining static methods. Such methods are part of a class, just like any method, though they may be used even without any such instantiated object. The function called static::theFunction[] behaves like “I will execute in the context of the class, which has been actually called by the outside world”.

    • Example:

    • Output:
      I am a Car!
      I am a Mercedes!

    PHP new self vs new static: Now that we changed the code in our example to use static instead of self, you can see the difference is that self references the current class, whereas the static keyword allows the function to bind to the calling class at runtime. The difference between self and static keywords is fairly easy to understand with an example.

    Output: In this example of code, f inherits both methods from g. The self invocation is bound to A because it is defined in g’s implementation of the method, whereas static is bound to the called class.

    gfg

    What is the difference between self and this in PHP?

    self is used to access static or class variables or methods and this is used to access non-static or object variables or methods. So use self when there is a need to access something which belongs to a class and use $this when there is a need to access a property belonging to the object of the class.

    What does self mean in PHP?

    PHP self refers to the class members, but not for any particular object. This is because the static members[variables or functions] are class members shared by all the objecxts of the class. Whereas, $this wil refer the member variables and function for a particular instance.

    What is static in PHP?

    The static keyword is used to declare properties and methods of a class as static. Static properties and methods can be used without creating an instance of the class. The static keyword is also used to declare variables in a function which keep their value after the function has ended.

    What is the difference between self and this?

    The keyword self is used to refer to the current class itself within the scope of that class only whereas, $this is used to refer to the member variables and function for a particular instance of a class.

    Chủ Đề