Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 覆盖构造_Php_Design Patterns_Architecture - Fatal编程技术网

Php 覆盖构造

Php 覆盖构造,php,design-patterns,architecture,Php,Design Patterns,Architecture,假设我有以下代码: abstract class AbstractCar { private $car; public function __construct(CarInterface $car) { $this->car = $car; } } class LamboCar extends AbstractCar { public function __construct(LamboCarInterface $car)

假设我有以下代码:

abstract class AbstractCar {
    private $car;

    public function __construct(CarInterface $car)
    {
        $this->car = $car;
    }
}

class LamboCar extends AbstractCar {
    public function __construct(LamboCarInterface $car)
    {
        parent::__construct($car);
    }
}

问题是:重写构造方法是否只是将
$car
类型从
CarInterface
更改为
LamboCarInterface
LamboCarInterface
扩展
CarInterface
)的错误做法?假设AbstractCar确实从
CarInterface
调用了一些方法,并且
LamboCar
LamboCarInterface

调用了一些方法,使您的
LamboCarInterface
继承
CarInterface
,并且您的
\uu构造应该接收
CarInterface
。不,这不是一个坏习惯。没问题,它已经继承了
CarInterface
。假设使用
LamboCar接口
重写构造的主要目的是,lambo有一些在
LamboCar
中使用的方法,并提取给定对象的类型。(
LamboCar
无法接收
CarInterface