将子类分配给父类中的变量

时间:2015-02-06 14:09:06

标签: php oop inheritance

我有这些相关的课程:

class cars {

    public $cars;

    public function addCar($name, $car)
    {
        $this->cars[$name] = $car;
    }

    public function getCars()
    {
        return $this->cars;
    }

    public function getCar($name)
    {
        return $this->cars[$name];
    }

    public function getParams()
    {
        return $this->params;
    }
}

$cars = new cars();

class bmw extends cars {

    private static $_instance = null;
    protected $params;

    function __construct()
    {
        $this->params['param'] = 'foo';
    }

    public static function init()
    {
        if (self::$_instance === null) {
            self::$_instance = new self;
        }

        return self::$_instance;
    }
}

$cars->addCar( 'bmw', bmw::init() );

基本上我需要从父类访问所有子类。并在这些已定义的子类中使用父类中定义的方法。添加新的子类时不应修改父类。

最后这应该是这样的:

foreach( $cars->getCars() as $car )
{
    foreach( $car->getParams() as $key => $param )
        echo "$key = $param";
}

这样做的正确方法是什么?

2 个答案:

答案 0 :(得分:2)

提供帮助真的很难,因为你不太清楚你想要实现的目标。

在我看来,你需要注册表类(carDealer),一个具有公共(对于每个孩子)方法的抽象类和一个子(Bmw)。

所以,比如:

// You seems to need what is called sometimes a Registry. 
// Something which deal with keeping and delivering a group of 'related' classes, as a register.
class CarsDealer
{
    public $cars;

    public function addCar($name, $car)
    {
        $this->cars[$name] = $car;
    }

    public function getCars()
    {
        return $this->cars;
    }

    public function getCar($name)
    {
        return $this->cars[$name];
    }
}

// then you need a basic contract for each concrete classes 
// that will have the same nature and so will extend it 
abstract class Car
{
    protected $params;

    public function getParams()
    {
        return $this->params;
    }
}

// finally the concrete class
class Bmw extends Car
{
    public function __construct($params = null)
    {
        $this->params['param'] = $params;
    }
}

$carsDealer = new CarsDealer();

$carsDealer->addCar('bmw', new Bmw('foo'));

foreach ($carsDealer->getCars() as $car)
{
    foreach ($car->getParams() as $key => $param) {
        echo "$key = $param";
    }
}

请注意一些基本规则/良好做法/惯例:

答案 1 :(得分:0)

只是另一种方法,如果你只需要得到这个'params': - )

class cars {

    public $cars;

    public function addCar($name, $car)
    {
        $this->cars[$name] = $car;
    }

    public function getCars()
    {
        return $this->cars;
    }

    public function getCar($name)
    {
        return $this->cars[$name];
    }

    public function getParams($obj)
    {
        return $obj->params;
    }


}

$cars = new cars();

class bmw extends cars {

    private static $_instance = null;
    protected $params;

    function __construct()
    {
        $this->params['param'] = 'foo';
    }

    public static function init()
    {
        if (self::$_instance === null) {
            self::$_instance = new self;
        }

        return self::$_instance;
    }
}

$cars->addCar( 'bmw', bmw::init() );

print_r( $cars->getParams($cars->getCar('bmw')));