Home > Backend Development > PHP Tutorial > Why Were Abstract Static Class Methods Abolished in PHP 5.2 ?

Why Were Abstract Static Class Methods Abolished in PHP 5.2 ?

Mary-Kate Olsen
Release: 2024-11-12 04:26:01
Original
368 people have browsed it

Why Were Abstract Static Class Methods Abolished in PHP 5.2 ?

PHP's Abolishment of Abstract Static Class Methods in 5.2

In PHP 5.2 and beyond, the use of abstract static methods within classes is prohibited. This decision stems from a combination of oversight, design flaws, and a misunderstanding of the self keyword.

The Problematic Context

Initially, PHP 5.0x and 5.1x mistakenly allowed the declaration of abstract static functions within classes. However, this was rendered useless due to the absence of late static bindings. As a result, code like the following would fail:

abstract class ParentClass {
    static function foo() {
        self::bar();
    }

    abstract static function bar();
}

class ChildClass extends ParentClass {
    static function bar() {
        echo "Hello, World!";
    }
}

ChildClass::foo();
Copy after login

In this example, the self::bar() call in foo() refers to the bar() method in ParentClass, even when foo() is invoked as a method of ChildClass. Consequently, the code generates a fatal error, indicating that the abstract method ParentClass::bar() cannot be called.

The Justification for the Warning

Given this limitation, PHP 5.2 rightfully issued a warning to discourage the use of abstract static methods. Since they served no practical purpose, any usage was likely due to a misunderstanding and was therefore considered bad code.

The Misunderstanding Resolved

PHP 5.3 introduced late static bindings, allowing methods in parent classes to access static methods in child classes. With this addition, the rationale for prohibiting abstract static methods became less compelling. However, the warning persisted due to a tenuous argument that exposing broken static methods could be problematic.

The Error and Its Resolution

Unfortunately, a misunderstanding by Rasmus Lerdorf, PHP's creator, led to the mistaken closure of a bug report advocating for the removal of the warning. Subsequently, the warning remained in place despite its lack of a solid justification.

The Future of Abstract Static Methods

Fortunately, PHP 7 has finally removed the warning, as proposed in RFC: Reclassify E_STRICT notices. With its abolition, developers can once again utilize abstract static methods without encountering spurious warnings.

The above is the detailed content of Why Were Abstract Static Class Methods Abolished in PHP 5.2 ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template