如何以适当的方式扩展一些php类,以便从子类访问其他子类的所有公共方法


How can extend in proper way some php classes for have access from child class to all public methods from other child class?

上面的类有什么问题?基本上,我想从奥迪,福特,欧宝类访问所有或两个3类(公共方法)。例如,在奥迪的一个对象方法中,可以访问欧宝的公共方法,如:

[在奥迪]函数otherPrices(){return$this->opel->getPrice();}

class Car {
    function Car() {}
}
class Model extends Car {
    public $audi;
    public $ford;
    public $opel;
    function Model() {
        parent::Car();
        $this->audi = new Audi();
        $this->ford = new Ford();
        $this->opel = new Opel();
    }
}
class Factory {
    public $audi;
    public $ford;
    public $opel;
    function Factory(){
        $model = new Model();
        $this->audi = $model->audi;
        $this->ford = $model->ford;
        $this->opel = $model->opel;
    }
}
class Audi extends Factory {
    public $price = 10;
    function Audi() {}
    function audiVsOpel(){
        return "A:" . ($this->price - $this->opel->price);
    }
}
class Ford extends Factory {
    public $price = 12;
    function Ford() {}
    function fordVsAudi(){
        return "B:" . ($this->price - $this->audi->price);
    }
}
class Opel extends Factory {
    public $price = 14;
    function Opel() {}
    function opelVsford(){
        return "C:" . ($this->price - $this->ford->price);
    }
}
/*
Here a implementation
*/
$factory = new Factory();
echo $factory->audi->audiVsOpel(); // want to echo -4
echo $factory->ford->fordVsAudi(); // want to echo 2
echo $factory->opel->opelVsford(); // want to echo 2

您可以在类Factory中使用getter:

public function getFactory($factory) {
    if (NULL === $this->{$factory}) {
        $this->{$factory} = new $factory;
    }
    return $this->{$factory};
}

然后在每个工厂类中获得不同的工厂对象,方法如下:

$this->getFactory('opel')->price

代码中的工作示例:

class Car {
    function Car() {}
}
class Model extends Car {
    public $audi;
    public $ford;
    public $opel;
    function Model() {
        parent::Car();
        $this->audi = new Audi();
        $this->ford = new Ford();
        $this->opel = new Opel();
    }
}
class Factory {
    public $audi = NULL;
    public $ford = NULL;
    public $opel = NULL;
    function Factory(){
        $model = new Model();
        $this->audi = $model->audi;
        $this->ford = $model->ford;
        $this->opel = $model->opel;
    }
    public function getFactory($factory) {
        if (NULL === $this->{$factory}) {
            $this->{$factory} = new $factory;
        }
        return $this->{$factory};
    }
}
class Audi extends Factory {
    public $price = 10;
    function Audi() {}
    function audiVsOpel(){
        return "A:" . ($this->price - $this->getFactory('opel')->price);
    }
}
class Ford extends Factory {
    public $price = 12;
    function Ford() {}
    function fordVsAudi(){
        return "B:" . ($this->price - $this->getFactory('audi')->price);
    }
}
class Opel extends Factory {
    public $price = 14;
    function Opel() {}
    function opelVsford(){
        return "C:" . ($this->price - $this->getFactory('ford')->price);
    }
}
/*
Here a implementation
*/
$factory = new Factory();
var_dump($factory);
echo $factory->audi->audiVsOpel(); // echo -4
echo $factory->ford->fordVsAudi(); // echo 2
echo $factory->opel->opelVsford(); // echo 2