php后期静态绑定和';自我';电话

php后期静态绑定和';自我';电话,php,late-static-binding,Php,Late Static Binding,我已经看过了,我知道它是如何运作的。 但这不是很令人困惑吗?使用self::someMethod()我们打算停止多态行为,我们希望不再依赖子类中可能的重写。但是这个例子(非常不自然,但仍然)表明,这种期望可能导致意外的bug。假设我们有类层次结构Shape->Rectangle->Square,以及其他用于计算面积的静态方法: abstract class Shape { //"abstract" `area` function which should be overriden by

我已经看过了,我知道它是如何运作的。 但这不是很令人困惑吗?使用
self::someMethod()
我们打算停止多态行为,我们希望不再依赖子类中可能的重写。但是这个例子(非常不自然,但仍然)表明,这种期望可能导致意外的bug。假设我们有类层次结构Shape->Rectangle->Square,以及其他用于计算面积的静态方法:

abstract class Shape {
    //"abstract" `area` function which should be overriden by children
    public static function area(array $args) {
       throw new Exception("Child must override it");
    }

    //We also provide checking for arguments number.
    static function areaNumArgs(){
      throw new Exception("Child must override it");
    }

    final static function checkArgsNumber (array $args) {
        return count($args) == static::areaNumArgs(); 
        //With 'static' we use late static binding
    }
}

class Rectangle extends Shape {
    static function areaNumArgs(){return 2;} //Arguments are lengths of sides

    static function area(array $args) {
        //Algorithm is stupid, but I want to illustrate result of 'self'
        $n = self::areaNumArgs(); 
        /*With 'self' we DON'T use polymorphism so child can 
        override areaNumArgs() and still use this method.
        That's the point of 'self' instead of 'static', right?*/
        $area = 1;
        for($i = 0; $i<$n; $i++) $area *= $args[$i];
        return $area;
    }

    //Let's wrap call to 'area' with checking for arguments number
    static function areaWithArgsNumberCheck (array $args)
    {
        if(self::checkArgsNumber($args)) 
            return self::area($args);
            //We use 'self' everywhere, so again no problem with 
            //possible children overrides?
        else
            return false;
    }
}

var_dump(Rectangle::areaWithArgsNumberCheck(array(3,2))); 
//output is 6, everything OK.

//Now we have Square class.
class Square extends Rectangle {
    static function areaNumArgs(){return 1;} 
    //For square we only need one side length

    static function area(array $args) {
        //We want to reuse Rectangle::area. As we used 'self::areaNumArgs', 
        //there is no problem that this method is overriden.
        return parent::area(array($args[0], $args[0]));
    }

    static function areaAnotherVersion(array $args) {
        //But what if we want to reuse Rectangle::areaWithArgsNumberCheck? 
        //After all, there we also used only 'self', right?
        return parent::areaWithArgsNumberCheck(array($args[0], $args[0]));
    }
}

var_dump(Square::area(array(3))); //Result is 9, again everything OK.

var_dump(Square::areaAnotherVersion(array(3))); //Oops, result is FALSE
抽象类形状{
//“abstract”`area`函数,应被子函数覆盖
公共静态功能区(数组$args){
抛出新异常(“子级必须覆盖它”);
}
//我们还提供了参数数量的检查。
静态函数areaNumArgs(){
抛出新异常(“子级必须覆盖它”);
}
最终静态函数checkArgsNumber(数组$args){
返回计数($args)=static::areaNumArgs();
//对于“static”,我们使用后期静态绑定
}
}
类矩形扩展形状{
静态函数areaNumArgs(){return 2;}//参数是边的长度
静态功能区(数组$args){
//算法很愚蠢,但我想说明“self”的结果
$n=self::areaNumArgs();
/*对于“self”,我们不使用多态性,所以孩子可以
重写areaNumArgs()并仍使用此方法。
这就是“自我”而不是“静态”的意义,对吗*/
$area=1;
对于($i=0;$i)