duanqian2368 2015-04-22 16:23
浏览 30
已采纳

OOP PHP:在子静态元素的声明中使用parent的static元素?

I'm trying to write a class that uses its parent's static declared array to add a new value to. Below is kind of a feel of how I'm trying to run it...

class SuperClass
{

protected static $array_name = array('value1');

}

class SubClass extends SuperClass 
{

protected static $array_name = array_push(parent::$array_name, 'value2');

}

Is there any way to properly implement this without a __construct() function?

I'm trying to implement a static working model for the SuperClass and its parents...

  • 写回答

1条回答 默认 最新

  • douyue1481 2015-04-22 16:58
    关注

    I'm not entirely sure if you want static classes completely, but this does what you want give or take :

    <?php
    class SuperClass
    {
        static $array_name = array('value1');
    }
    
    class SubClass extends SuperClass
    {
    
        static $array_name = 'foo';
        function __construct(){
    
            self::$array_name = array_push(parent::$array_name, 'value2');
        }
    }
    $foo = new SubClass();
    var_dump($foo::$array_name);  // prints INT 2 - as push returns number of elements in array.
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1