"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Why Did PHP 5.2 Disallow Abstract Static Class Methods?

Why Did PHP 5.2 Disallow Abstract Static Class Methods?

Published on 2024-11-14
Browse:635

Why Did PHP 5.2 Disallow Abstract Static Class Methods?

PHP 5.2 Strict Mode: Why the Disallowance of Abstract Static Class Methods?

In PHP 5.2, enabling strict warnings may trigger a familiar warning: "Static function should not be abstract". This warning stems from a change introduced in PHP 5.2, which disallows abstract static class methods.

The Reason: A Historical Oversight

PHP 5.2 initially lacked late static bindings, rendering abstract static functions useless. As the self keyword in abstract static methods referred to the parent class, not the child class on which the method was invoked, such methods had no practical significance. Thus, the warning was intended to catch and prevent potential coding errors.

Late Static Bindings and the Flawed Justification

In PHP 5.3, late static bindings were introduced, allowing static methods of child classes to be called from parent methods using the static keyword. This removed the technical barrier that had made abstract static methods useless. However, the warning persisted in PHP 5.2 based on a weak argument: that exposing abstract static methods could result in broken public methods.

A False Perception

The justification for the warning was flawed. Abstract static methods could coexist with late static bindings, providing a valid use case for invoking static methods of child classes from parent methods.

Confusion and an Inaccurate Response

A PHP bug report requesting the removal of the warning due to the addition of late static bindings received a dismissive response from Rasmus Lerdorf. He erroneously stated that abstract static methods with late static bindings were functional, which was incorrect as they still triggered the warning. This confusion and lack of clarity led to the persistence of the warning.

The Resolution in PHP 7

Fortunately, PHP 7 has finally rectified this issue, reclassifying this warning to a mere notice. With the introduction of PHP 7, abstract static methods are fully supported and can be safely used without triggering any warnings.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3