如何以正确的方式扩展某些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类(公共方法)。例如,在Audi对象方法之一中,可以访问opel公共方法,例如:

[奥迪]函数otherPrices()返回$this->opel->getPrice();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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

您可以在类工厂中使用getter:

1
2
3
4
5
6
public function getFactory($factory) {
    if (NULL === $this->{$factory}) {
        $this->{$factory} = new $factory;
    }
    return $this->{$factory};
}

然后以这种方式在每个工厂类中获取不同的工厂对象:

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

您的代码的工作示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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