PHP class methods and variables can be declared as static similarly to how Java static methods and variables work. As often as possible I try to stay away from anything static in PHP, since it can lead to confusion, bad program design, and in some cases memory management problems. But sometimes it actually does make sense and more often than not just getting things done is more important than doing things correctly.
However, PHP 5.3 introduced a lot of cool new features like namespaces and something called “Late Static Bindings” via a new keyword. But what is Late Static Binding and how does it differ from how PHP before 5.3 works? The reason the new static keyword was added is to allow for intuitive static references involving inheritance.
Before 5.3, PHP used early static binding: the process of resolving any static references to the class where it was initially declared. Clear as mud right? This is best shown by the code example from the PHP documentation.
<?php
class A {
public static function who() {
echo __CLASS__;
}
public static function test() {
self::who();
}
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::test();
?>
The above code will output A not the (maybe) expected B. Replacing self with static in the example will output B, but only in 5.3 and above.
Since none of the servers I work with have 5.3, and some are even lacking 5.2, I cannot take use of the static keyword in my code. But even so, I feel like I understand PHP better now that new feature is available since this clearly shows that using self in any version will use “Early Static Binding.”
Why are you complaining about lacking PHP 5.2? Isn’t common business practice to write code compatible with 5.0?
[...] this link: Late Static Bindings in PHP « Joseph Piché [...]
[...] Read this article: Late Static Bindings in PHP « Joseph Piché [...]
Хорошего Вам дня! ian@elektrashop.ru” rel=”nofollow”>……
с ув….
Добрый вечер! mason@sportbul.ru” rel=”nofollow”>……
С уважением,…