PHP: How to Pass child class __construct() arguments to parent::__construct()?

前端 未结 7 1258
-上瘾入骨i
-上瘾入骨i 2020-12-14 00:17

I have a class in PHP like so:

class ParentClass {
    function __construct($arg) {
        // Initialize a/some variable(s) based on $arg
    }
}
         


        
7条回答
  •  忘掉有多难
    2020-12-14 00:42

    This can be done in PHP >= 5.6 without call_user_func_array() by using the ... (splat) operator:

    public function __construct()
    {
        parent::__construct(...func_get_args());
    }
    

提交回复
热议问题