【发布时间】:2015-03-11 08:58:58
【问题描述】:
我使用这个网站的多个构造。
我根据需要对其进行了修改。
我遇到致命错误:
Missing argument 3 for ChildClass::__construct2()
还有连锁错误...
Missing argument 4 for ChildClass::__construct2()
Missing argument 5 for ChildClass::__construct2()
Undefined variable: c
Undefined variable: d
并且两个构造函数都有部分相同的代码。我怎样才能把它放在共同的__construct。
$arr = array (
"key1" => "val1",
"key2" => "val2"
);
$demo = new Demo("stack", $arr );
class ParentClass {
function __construct($var1 = "1", $var2 = "2"){
// distinctly different code
}
}
class ChildClass extends ParentClass {
function __construct(){
$a = func_get_args();
$i = func_num_args();
if (method_exists($this,$f='__construct'.$i)) {
call_user_func_array(array($this,$f),$a);
}
}
function __construct1( $a, array $e ) {
$this->ex = $a;
$this->ex3 = $e['key1'];
$this->ex4 = $e['key2'];
}
function __construct2( $a, $b, $c, $d, array $e ) {
$this->ex = $a + 1 - $v + $c; //example
$this->ex2 = $d;
$this->ex3 = $e['key1'];
$this->ex4 = $e['key2'];
}
}
并且两个构造函数都有部分相同的代码。我怎样才能把它放在共同的__construct。
谢谢。
【问题讨论】:
-
您的
ChildClass构造函数无效:它破坏了继承的合同。也不要在您自己的方法名称中使用__前缀,它们仅用于魔术方法(构造函数、析构函数、setter、getter 等)
标签: php inheritance constructor default-constructor