New self vs. new static in PHP

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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    class Car
    {
        public static function model()
        {
            self::getModel();
        }
      
        protected static function getModel()
        {
            echo "I am a Car!";
        }
      
    }
      
    class Mercedes extends Car
    {
       
        protected static function getModel()
     {
            echo "I am a Mercedes!";
        }
       
    }
    Car::model();
    echo("\n");
    Mercedes::model();
    ?>

    chevron_right

    
    

  • 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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    class Car
    {
        public static function model()
        {
             static::getModel();
        }
       
        protected static function getModel()
        {
            echo "I am a Car!";
        }
    }
      
    class Mercedes extends Car
    {
       
        protected static function getModel()
     {
            echo "I am a Mercedes!";
        }
       
    }
    Car::model();
    echo("\n");
    Mercedes::model();
    ?>

    chevron_right

    
    

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
class g {
      
    /* The new self */
    public static function get_self() {
    return new self();
    }
   
    /* The new static */
    public static function get_static() {
    return new static();
    }
}
   
class f extends g {}
   
echo get_class(f::get_self()); // g
echo get_class(f::get_static()); // f
echo get_class(g::get_self()); // g
?>

chevron_right


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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.