Open In App

New self vs. new static in PHP

Last Updated : 21 May, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:




    <?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();
    ?>

    
    

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




    <?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();
    ?>

    
    

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




<?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
?>


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


Similar Reads

Difference between self::$bar and static::$bar in PHP
self keyword: It is a PHP keyword that represents the current class and used to access static class variables or static variables because these members belong to a class rather than the object of that class. Example: &lt;?php // Declaring parent class class demo { public static $bar = 10; public static function func() { echo self::$bar . &quot;\n
2 min read
When to use self over $this in PHP ?
The self and this are two different operators which are used to represent current class and current object respectively. 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 th
2 min read
How to create a PHP form that submit to self ?
Forms can be submitted to the web page itself using PHP. The main purpose of submitting forms to self is for data validation. Data validation means checking for the required data to be entered in the form fields. PHP_SELF is a variable that returns the current script being executed. You can use this variable in the action field of the form. The act
3 min read
Comparison between static and instance method in PHP
Static methods The static method in PHP is same as other OOP languages. Static method should be used only when particular data remains constant for the whole class. As an example, consider that some programmer is making the data of a college and in that every object needs getCollegeName function that returns the same college name for all objects as
3 min read
Static Function in PHP
In certain cases, it is very handy to access methods and properties in terms of a class rather than an object. This can be done with the help of static keyword. Any method declared as static is accessible without the creation of an object. Static functions are associated with the class, not an instance of the class. They are permitted to access onl
3 min read
When to use static vs instantiated classes in PHP?
Prerequisite - Static Function PHP In PHP, we can have both static as well as non-static (instantiated) classes. Static class Introduction: A static class in PHP is a type of class which is instantiated only once in a program. It must contain a static member (variable) or a static member function (method) or both. The variables and methods are acce
5 min read
What is Late Static Bindings in PHP ?
In PHP, programs are saved and then directly run on the browser, the script is executed through a web server and we get the output. We don't compile PHP programs manually but that does not mean it is never compiled. The PHP interpreter does that for you and runs it. So there are two phases, first, compile-time and second run time. During the compil
4 min read
How to create static classes in PHP ?
A class is a user-defined data type that holds its own data members and member functions that can be accessed and used by creating one or more instances of that class. Every time a class is instantiated, the values that it holds are different and unique to the particular instance or object and not to that class. Static Classes brings forth a proper
2 min read
Why does PHP 5.2+ disallow abstract static class methods ?
Before we answer this question, you must have a clear definition of what exactly is an abstract class, an abstract method and a static method. Abstract Class: In the Object-Oriented programming paradigm, abstraction refers to the process of hiding the internal implementation details of any program and only showing the functionality of the program t
2 min read
What is Static Keyword in PHP?
In PHP, the static keyword is used to declare class members (properties and methods) that belong to the class itself rather than to instances of the class (objects). Static members are shared across all instances of the class and can be accessed without creating an object of the class. Syntax:class MyClass { public static $count = 0; public static
1 min read
Article Tags :