new self:
- self points to the class in which it is written.
- self refers to the same class in which the new keyword is actually written.
New static
- static, in PHP 5.3’s late static bindings, refers to whatever class in the hierarchy you called the method on.
what is the difference between self and static?
- The differences between self and static are fairly easy to understand with some good examples.
- So, let’s take a look at some actual code. Suppose we have the following class – called Car – which has two simple methods called model and getModel. Note the use of the self keyword:
Example of self in PHP:
[ad type=”banner”]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:
The output after calling the model function in the Car class will be:
[ad type=”banner”]- The self keyword simply makes a call to the getModel function that belongs to the Car class, which just outputs the text “I am a Car!” to the page.
- Let’s say that we decide to add a new class, called Mercedes, that derives from the Car class.
Here is what it looks like:
- Because the Mercedes class derives from the Car class, it will inherit the model function that is defined in the Car class.
So, what do you think will happen if we call the static model function, but this time we use the Mercedes class instead – like this:
Well, the output from the function call above may not be what you expect – this is what the output looks like:
- You may have been expecting the Mercedes::model(); call to have output “I am a Mercedes!”. So, what is going on here?
Explaining self:
- 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 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 is 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 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.
- This behavior may be considered undesirable because it is not polymorphic, and is not aligned with object oriented design principles.
- But, there is an alternative solution that can get us that kind of behavior – and it involves using the static keyword in a different way from how you may normally use it.
late static or new static binding:
- In PHP 5.3, a new feature called late static bindings was added – and this can help us get the polymorphic behavior that may be preferable in this situation.
- In simplest terms, late static bindings means that a call to a static function that is inherited will “bind” to the calling class at runtime.
- So, in our example above, if we use late static binding it would mean that when we make a call to “Mercedes::model();”, then the getModel function in the Mercedes class will be called instead of the getModel function in the Car class.
- Mercedes is of course the calling class in our example.
Example of late static binding in PHP
- Now, the question is how can we actually make late static binding work for us? Well, all we have to do is replace the “self::getModel();” call inside the Car class with “static::getModel();” instead.
- So, this is what our new Car class will look like – note that we do not have to make any change to the Mercedes class:
Now, if we make this call:
Our output will be this:
[ad type=”banner”]Late static binding was not possible before PHP 5.3:
- Note that before PHP 5.3 late static binding was not possible – and trying to run the code above in any version of PHP before 5.3 will return an error.
PHP self versus 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.