Why PHP allows abstract static functions

Consider the following code:

abstract class ExampleClass
{
    public static function regularStaticFunction()
    {
        return static::abstractStaticFunction();
    }

    abstract protected static function abstractStaticFunction();
}

ExampleClass::regularStaticFunction();

The PhpStorm IDE puts a warning in an ad abstractStaticFunctionthat states:

Strict PHP standards: The static function abstractStaticFunction does not have to be abstract.

A static function should not be abstract.

However, PHP continues to run the program while parsing this class and outputs the following:

Strict PHP standards: Static function ExampleClass :: abstractStaticFunction () should not be abstract in php shell code on line 7

It seems to me that since PHP allows calls to static functions in abstract classes, defining an abstract static function in an abstract class should not be possible.

PHP , ?

+8
2

:

PHP 53081, , static::foo() . ( PHP) , , , . , , :

, :

abstract class cA
{
      //static function A(){self::B();} error, undefined method
      static function A(){static::B();} // good
      abstract static function B();
}

class cB extends cA
{
    static function B(){echo "ok";}
}

cB::A();

, .

:(

?

abstract class cA {
      static function A(){static::B();}
      abstract static function B();
}

class cB extends cA {
    static function B(){echo "ok";}
}

cB::A();

. , , self :: B(), static :: B() .

, " ", ; , . , . , "".

. , - , , . , , . .

, PHP 7 PHP RFC: E_STRICT. , PHP 7 , abstract static .

+18

! , , , , , , , , Java #.

. , - -, - API. API , , .

PHP, , -, ...

abstract class ApiObject {
    /** The REST resource URL for this object type in the Foo API. */
    abstract static function fooApiResourceUrl();

    /** The REST resource URL for this object type in the Bar API. */
    abstract static function barApiResourceUrl();

    /** Given an XML response from the Foo API representing an object of this
        type, construct an instance. */
    abstract static function fromFooXml($xml);

    /** Given a JSON response from the Bar API representing an object of this
        type, construct an instance. */
    abstract static function fromBarJson($json);

    /** Serialize this object in the XML format that the Foo API understands */
    abstract function toFooXml();

    /** Serialize this object as JSON that the Bar API understands */
    abstract function toBarJson();
}

... , , , API , API. , , :

// Ensure that all instances of these types that exist in the Foo API also
// exist in the Bar API:
$classesToSync = ['Widget', 'Frobnicator', 'Lead', 'Invoice'];
foreach ($classesToSync as $apiObjectClass) {
    $fooObjXmls = httpGetRequest($apiObjectClass::fooApiResourceUrl());
    foreach ($fooObjXmls as $fooObjXml) {
        $fooObj = $apiObjectClass::fromFooXml($fooObjXml);
        $json = $fooObj->toBarJson();
        httpPutRequest($apiObjectClass::barApiResourceUrl(), $json);
    }
}

? ; , , , , JSON XML. , , .

, , , , , , . , , - , , , . , , , , , PHP . .

0

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


All Articles