📜 ⬆️ ⬇️

Later Static Linking in PHP (Part I)

php Later Late Static Binding (LSB) has been a hot topic of discussion for the last three years in PHP developer circles (and finally we got it in PHP 5.3). But why is it needed? In this article, it will be considered just how later static binding can greatly simplify your code.

At a meeting of PHP developers, which was held in Paris in November 2005, the topic of late static binding was officially discussed by the main development team. They agreed to implement it, along with many other topics that were on the agenda. Details should have been agreed in open discussions.

Two years have passed since static binding was announced as the coming chip. Finally, LSB is available for use in PHP 5.3. But this event passed unnoticed by developers using PHP, from the notes there is only a page in the manual .

In short, the new late static binding functionality allows objects to also inherit methods from parent classes, but in addition allows inherited methods to have access to static constants, methods, and properties of the descendant class, and not just the parent class. Let's look at an example:
')
 class Beer {
     const NAME = 'Beer!';
       public function getName () {
           return self :: NAME;
     }
 }
 class Ale extends Beer {
	 const NAME = 'Ale!';
 }

 $ beerDrink = new Beer;
 $ aleDrink = new Ale;

 echo "Beer is:".  $ beerDrink-> getName (). "\ n";
 echo "Ale is:".  $ aleDrink-> getName (). "\ n";


This code will produce the following result:

 Beer is: Beer!
 Ale is: Beer!


The Ale class inherited the getName () method, but at the same time self still points to the class in which it is used (in this case, this is the Beer class). This remained in PHP 5.3, but the word static was added. Again, consider an example:

 class Beer {
   const NAME = 'Beer!';
   public function getName () {
	   return self :: NAME;
   }
   public function getStaticName () {
	   return static :: NAME;
   }
 }

 class Ale extends Beer {
   const NAME = 'Ale!';
 }

 $ beerDrink = new Beer;

 $ aleDrink = new Ale;

 echo "Beer is:".  $ beerDrink-> getName (). "\ n";
 echo "Ale is:".  $ aleDrink-> getName (). "\ n";

 echo "Beer is actually:".  $ beerDrink-> getStaticName (). "\ n";
 echo "Ale is actually:".  $ aleDrink-> getStaticName (). "\ n";


The new static keyword indicates that you need to use the constant of the inherited class, instead of the constant that was defined in the class where the getStaticName () method is declared . The word static was added to implement the new functionality, and for backward compatibility, self works just like in previous versions of PHP.

Internally, the main difference (and, in fact, the reason why the binding was called late) between these two access methods, is that PHP will determine the value for self :: NAME during the “compilation” (when PHP symbols are converted into native code that will be processed with the Zend engine), and for static :: NAME, the value will be determined at launch time (at the moment when the machine code will be executed in the Zend engine).

This is another tool for PHP developers. In the second part we consider how it can be used for good.

UPDATE: The second part of the article. A practical example.

VIA: Later Static Linking in PHP (Part I)

Translation of this: Late Static Binding: a practical example

Source: https://habr.com/ru/post/23066/


All Articles