2015-02-06 70 views
-2

我有這些相關的類:指定子類中的變量父

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"; 
} 

什麼是做到這一點的正確方法?

+0

'基本上我需要從父class'訪問所有子類,所以你想重塑OOP?這是非常令人難以置信的:-) – Alex 2015-02-06 14:13:57

+0

有一些真正奇怪的事情。 「Cars」是一個數組,但擴展它的「bmw」是一個單獨的實例。這是不是很清楚你在這裏做什麼。 – Erik 2015-02-06 14:13:58

+0

我想要在var中獲取所有類汽車的子實例,然後使用在這些實例中的汽車類中定義的方法。 – code9 2015-02-06 14:21:52

回答

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"; 
    } 
} 

請注意一些基本規則/良好做法/慣例:

+0

我應該爲此使用註冊表。感謝您的幫助。 – code9 2015-02-06 14:54:25

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')));