如何以适当的方式扩展某些 php classes 以便从子 class 访问其他子 class 的所有 public 方法?
How can extend in proper way some php classes for have access from child class to all public methods from other child class?
以上有什么问题类?基本上我想从奥迪、福特、欧宝 类 访问所有或两者 3 类 (public 方法)。
例如,在其中一种奥迪对象方法中可以访问欧宝 public 方法,例如:
[奥迪] function 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
您可以在 class 工厂中使用 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
以上有什么问题类?基本上我想从奥迪、福特、欧宝 类 访问所有或两者 3 类 (public 方法)。 例如,在其中一种奥迪对象方法中可以访问欧宝 public 方法,例如:
[奥迪] function 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
您可以在 class 工厂中使用 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