【问题标题】:Missing Argument in for multiple __construct多个 __construct 中缺少参数
【发布时间】: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


【解决方案1】:

这是因为 __construct2 中的 2 告诉它需要 2 个参数,但它需要 5 个。将名称更改为 __construct5

要将相同的代码放在一个地方,请将其设置为将从两个构造函数中调用的单独方法,完整代码:

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 __construct2( $a, array $e ) { // rename method: 1 => 2
        $this->ex = $a;

        $this->setE($e);
    }

    function __construct5( $a, $b, $c, $d, array $e ) { // rename method: 2 => 5
        $this->ex = $a + 1 - $v + $c; //example
        $this->ex2 = $d;

        $this->setE($e);
    }

    private function setE(array $e) {
        $this->ex3 = $e['key1'];
        $this->ex4 = $e['key2'];
    }
}

【讨论】:

  • 非常感谢。我不专心。
【解决方案2】:

__construct 后面指定的数字告诉它有多少个参数,除了 .您在 __construct 之后指定了 2 ,但您给了它 5 个参数,因此它给出了错误。将 2 改为 5。同样将 __construct1 重命名为 __construct2 使用下面的代码

$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 __construct2( $a, array $e ) { 
    $this->ex = $a;

    $this->setE($e);
}
    function __construct5( $a, $b, $c, $d, array $e ) {
        $this->ex = $a + 1 - $v + $c; //example
        $this->ex2 = $d;

        $this->ex3 = $e['key1'];
        $this->ex4 = $e['key2'];
    }
}

希望对你有帮助

【讨论】:

  • 谢谢。现在我不能在我的代码中引用 this->ex 注意:未定义的属性:ChildClass::$ex
  • @user3679891 更新了我的答案看看吧
猜你喜欢
  • 2014-06-03
  • 1970-01-01
  • 2017-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多