var_dump($this->tracks); } } class CD extends baseCD { public $type = 'cd'; } //增强型CD类, 与标准CD的唯一不同是写至CD的第一个track是数据track("DATA TRACK") class enhadcedCD extends baseCD { public $type = 'enhadced'; } //CD工厂类,实现对以上两个类具体实例化操作 class CDFactory { public static function create($type) { $class = strtolower($type) . "CD"; return new $class(); } } //实例操作 $type = "enhadced"; $objCd = CDFactory::create($type); $tracksFromExternalSource = array("What It Means", "Brr", "Goodbye"); $objCd->setBand("Never Again"); $objCd->setTitle("Waste of a Rib"); foreach ($tracksFromExternalSource as $track) { $objCd->addTrack($track); } $objCd->printTrack();
$this->tracks[] = 'DATA TRACK'; } public function setTitle($title) { $this->title = $title; } public function setBand($band) { $this->band = $band; } public function addTrack($track) { $this->tracks[] = $track; } } class CDFactory { public static function create($type) { $class = strtolower($type) . "CD"; return new $class(); } } $type = 'enhanced'; $cd = CDFactory::create($type); $cd->setBand($band); $cd->setTitle($title); foreach ($tracksFromExternalSource as $track) { $cd->addTrack($track); } print_r($cd);