Late Static Binding: Understanding "self" vs. "static"
In the context of converting a PHP 5.3 library to PHP 5.2, the use of late static binding via "return new static($options)" poses compatibility challenges. Replacing it with "return new self($options)" will not yield the same results. PHP 5.2 lacks support for late static binding.
Understanding the Differences
To clarify, the difference between "self" and "static" lies in their class bindings:
Example:
class A { public static function get_self() { return new self(); } } class B extends A { public static function get_self() { return new self(); } } $a_self = A::get_self(); $b_self = B::get_self(); // $a_self is an object of class A // $b_self is an object of class B
In conclusion, when converting code that uses late static binding to PHP 5.2, consider revising the logic to remove this feature. "self" can be used as a workaround in some cases, but it's important to understand its limited binding behavior.
The above is the detailed content of PHP 5.2 to 5.3 Migration: How Do 'self' and 'static' Differ in Late Static Binding?. For more information, please follow other related articles on the PHP Chinese website!