在过去的几个月里,我在学习纯粹的oop方面走了很长的路,现在我正在将设计模式应用到我的工作中!所以我不得不扩展我的 PHP知识,我正在使用接口,扩展它们,然后为这些接口实现类.我的
interface Car { function doGeneralCarStuff(); vinNumber =; } interface CompactCar extends Car { static $numCompactCars; function doCompactCarStuff(); } class HondaCivic implements CompactCar { function doGeneralCarStuff() { //honk horn , blink blinkers, wipers on, yadda yadda } function doCompactCarStuff() { //foo and bar and foobar } } class ToyotaCorolla implements CompactCar { function doGeneralCarStuff() { //honk horn , blink blinkers, wipers on, yadda yadda } function doCompactCarStuff() { //foo and bar and foobar } } myCar = new HondaCivic(); myFriendCar = new ToyotaCorolla();
好的,现在让我说我想了解一下我的本田扩展的接口之一,即CompactCar接口.我想知道有多少紧凑型轿车($numCompactCars)已经创建.我是新手(对我来说很深:p)OOP,所以如果我没有正确地做这件事,请提供指导.非常感谢!
如果将“新车”存储在一个数组中,您可以轻松地遍历它们并检查是否实现了给定的接口.就像是:$cars['myCar'] = new HondaCivic(); $cars['myFriendCar'] = new ToyotaCorolla(); $compact_counter = 0; foreach ($cars as $car) if ($car instanceof CompactCar) $compact_counter ++;
$compact_counter将有多少紧凑型轿车已经实施.