What exactly are late static bindings in PHP?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
The simplest example to show the difference.
Note, self::$c
Late static binding, note static::$c
I'm quoting from the book: "PHP Master write cutting-edge code".
Feel free to take a look at the official php documentation as well: http://php.net/manual/en/language.oop5.late-static-bindings.php
Example:
In the code above you can see two classes
Animal
which is the parent class andBird
which is the child class. BothAnimal
andBird
have agetAnimalName()
andgetWeight()
method. The superclassAnimal
has two methods:StaticCall()
andSelfCall()
.The method
StaticCall()
invokesgetAnimalName()
by using thestatic
keyword.The method
SelfCall()
invokesgetWeight()
by using theself
keyword.The question we now have is: in which context is
getAnimalName()
executed?The answer:
static::getAnimalName()
identifies the context and invokes the method within that context.If you invoke
Bird::StaticCall()
the code will executeStaticCall()
which is inAnimal
. Thenstatic::getAnimalName()
will invoke and execute fromBird
the methodgetAnimalName()
.This differs from
self::
, becauseself::
always invokes the method within the objectself
is defined in. So ifself::getWeight()
is defined in objectAnimal
in methodSelfCall()
andBird::SelfCall()
would be called thenself::getWeight()
invokesgetWeight()
in the context of theAnimal
object.There is not very obvious behavior:
The following code produces 'alphabeta'.
However, if we remove the declaration of the classname function from the beta class, we get 'alphaalpha' as the result.
For example:
As of PHP 5.3.0, PHP implements a feature called late static binding which can be used to reference the called class in the context of static inheritance.
Late static binding tries to solve that limitation by introducing a keyword that references the class that was initially called at runtime. It was decided not to introduce a new keyword, but rather use
static
that was already reserved.Let's see an example:
late static bindings
work by storing the class named in the last "non-forwarding call". In case of static method calls, this is the class explicitly named (usually the one on the left of the :: operator); in case of non-static method calls, it is the class of the object.A "forwarding call" is a static one that is introduced by
self::
,parent::
,static::
, or, if going up in the class hierarchy,forward_static_call()
.The function
get_called_class()
can be used to retrieve a string with the name of the called class andstatic::
introduces its scope.You definitely need to read Late Static Bindings in the PHP manual. However, I'll try to give you a quick summary.
Basically, it boils down to the fact that the
self
keyword does not follow the same rules of inheritance.self
always resolves to the class in which it is used. This means that if you make a method in a parent class and call it from a child class,self
will not reference the child as you might expect.Late static binding introduces a new use for the
static
keyword, which addresses this particular shortcoming. When you usestatic
, it represents the class where you first use it, ie. it 'binds' to the runtime class.Those are the two basic concepts behind it. The way
self
,parent
andstatic
operate whenstatic
is in play can be subtle, so rather than go in to more detail, I'd strongly recommend that you study the manual page examples. Once you understand the basics of each keyword, the examples are quite necessary to see what kind of results you're going to get.