An alternative model for abstract methods of a static class of PHP

Ok, so I read a lot of posts about this here, but I think this is a specific and new question.

I want the functionality of what many of us here incorrectly call the "abstract static method". That is: I want a class that insists on classes that extend it to implement certain static methods .

There seem to be two ways around the issue under discussion, which does not allow abstract static, but both seem inelegant.

Problem

The following error is generated: "Strict PHP standards: the static function A :: fn () should not be abstract."

<?php
abstract class A
{
     abstract static public function fn();
}

class B extends A
{
    static public function fn()
    {
         // does something on class vars etc.
    }
}

Solution 1: use interfaces

<?php
interface iA {
    static function fn();
}


abstract class A implements iA 
{
} // obviously this would have other stuff in it in reality

class B extends A
{
    static public function fn()
    {
         // does something on class vars etc.
    }
}

, (), . , , , ; , A .

, , "".

2:

abstract class A
{
     static public function fn()
     {  
         throw new Exception(get_called_class() . " must implement fn()");
     }

}

class B extends A
{
    static public function fn()
    {
         // does something on class vars etc.
    }
}

, fn() . . , .

- , ? , , , .

+5
2

, , . A::fn() B::fn() - , . A::fn() abstract.

, ? - , .

+2

, ,

abstract class A
{
     abstract static public function fn();
}

class B extends A
{
    static public function fn()
    {
         print 1;
    }
}

B::fn();

1 . . php ?

0

All Articles