0
Class a {
        public function __construct($a){
                $this->age = $a;
        }
}

Class b extends a {
        public function printInfo(){
                echo 'age: ' . $this->age . "\n";
        }
}

$var = new b('age');
$var->printInfo();

我了解这段代码是如何工作的,但是是否可以将参数传递给类和父类的构造函数?

我在下面的尝试导致错误

Class a {
        public function __construct($a){
                $this->age = $a;
        }
}

Class b extends a {
        public function __construct($name){
                $this->name = $name;
        }
        public function printInfo(){
                echo 'name: ' . $this->name . "\n";
                echo 'age: ' . $this->age . "\n";
        }
}

$var = new b('name', 'age');
$var->printInfo();
?>
4

5 回答 5

3

是的,您只需要使用该parent::__construct()方法。

像这样:

class a{        

    /**
     * The age of the user
     *
     * @var integer
     */
    protected $age;

    function __construct($a){
        $this->age = $a;
    }

}

class b extends a{        

    /**
     * The name of the user
     *
     * @var string
     */
    protected $name;

    function __construct($name,$age){
        // Set the name
        $this->name = $name;

        // Set the age
        parent::__construct($age);
    }

    public function printInfo(){
        echo 'name: ' . $this->name . "\n";
        echo 'age: ' . $this->age . "\n";
    }
}

$var = new b('name','age');
$var->printInfo();

只需确保将变量设置为公共或受保护!

于 2013-04-30T07:08:33.743 回答
0

这是应该怎么做的:

<?php
class a {
    private $age;
    public function __construct($age){
            $this->age = $age;
    }
    public function getAge()
    {
        return $this->age;
    }
}

class b extends a {
    private $name;

    public function __construct($age, $name){
        parent::__construct($age);
            $this->name = $name;
    }
    public function printInfo(){
            echo 'name: ' . $this->name . "\n";
            echo 'age: ' . $this->getAge() . "\n";
    }
}

$b = new b(20, "Bob");
$b->printInfo();
?>
于 2013-04-30T07:12:21.490 回答
0

您可以将值传递给父构造函数,但您的做法是错误的,

$var = new b('name', 'age');

就好像子类在其构造函数中接受两个参数,但实际上它只有一个参数。

您可以将参数传递给父构造函数,如下所示

parent::__construct($var);

所以把你的b类改成这个

Class b extends a {
    public function __construct($name, $age){
            $this->name = $name;
            parent::__construct($age);
    }
    public function printInfo(){
            echo 'name: ' . $this->name . "\n";
            echo 'age: ' . $this->age . "\n";
    }
}
于 2013-04-30T07:08:25.173 回答
0

是的,您可以将参数传递给类以及父类

Class a {
        public function __construct($age){
                $this->age = $a;
        }
}

Class b extends a {
        public function __construct($name,$age){
                parent::__construct($age);
                $this->name = $name;
        }      
}

$var = new b('name', 'age');

?>
于 2013-04-30T07:09:29.937 回答
0

只需在孩子中调用 parent::__construct 即可。例如

class Form extends Tag
{
    function __construct()
    {
        parent::__construct();
        // Called second.
    }
}
于 2013-04-30T07:11:15.423 回答