php - 带有关键字 Interface , extends , implements 的面向对象的 PHP

标签 php oop

在过去的几个月里,我在学习纯 oop 方面取得了长足的进步,现在我正在将设计模式应用到我的工作中!所以我不得不扩展我的 php 知识,我正在使用接口(interface),扩展它们,然后为这些接口(interface)实现类。我的问题是关于从扩展另一个接口(interface)的接口(interface)构造一个类,例如:

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

好的,现在假设我想了解一些关于我的本田扩展的接口(interface)之一,即 CompactCar 接口(interface)。我想知道已经制造了多少辆紧凑型汽车 ($numCompactCars)。我是深度(对我来说很深:p)OOP 的新手,所以如果我没有正确执行此操作,请提供指导。非常感谢!

最佳答案

工厂模式的唯一缺点是您可以随时绕过它 - 即 new HondaCivic() 会打乱汽车数量。

这里有一些更强大的东西:

<?php

interface Car
{
  function doGeneralCarStuff();
}

// Declare as abstract so it can't be instantiated directly
abstract class CompactCar
{
   // Increment car count
   function __construct() {
        CompactCar::$numCompactCars++;   
   }
   function __clone() {
       CompactCar::$numCompactCars++; 
   }
   // Decrement car count
   function __destruct() {
        CompactCar::$numCompactCars--;   
   }
   // Prevent external modification of car count
   protected static $numCompactCars;
   // Get the current car count
   static public function getCount() {
        return CompactCar::$numCompactCars;   
   }
   // Require a compact car function for descendant classes
   abstract public function doCompactCarStuff();
}

// Extend and implement
class HondaCivic extends CompactCar implements Car
{
   function doGeneralCarStuff() { }
   function doCompactCarStuff() { }
}

// Extend and implement
class ToyotaCorolla extends CompactCar implements Car
{
   function doGeneralCarStuff() { }
   function doCompactCarStuff() { }
}

$myCar = new HondaCivic();  // 1 car 
$myFriendCar = new ToyotaCorolla();  // 2 cars
printf("Number of compact cars: %d\n", CompactCar::getCount());
$myCar = new HondaCivic();  // still only 2 cars
unset($myFriendCar);  // one car left!
printf("Number of compact cars: %d\n", CompactCar::getCount());
$myCar2 = clone $myCar; // now we have two cars again
printf("Number of compact cars: %d\n", CompactCar::getCount());
CompactCar::$numCompactCars = 1000;  // sorry, you can't do that!

关于php - 带有关键字 Interface , extends , implements 的面向对象的 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5348526/

相关文章:

java - 只能从数组中索引 0 处删除对象

没有对象的 MATLAB 构造函数/接口(interface)类

java - 返回 OOP 中的计算值

php - 如何在 WooCommerce 中隐藏免费产品的支付网关?

php - 这是使用 mysql 的好方法吗?

c++ - 切片在 C++ 中意味着什么?

java - 生成某些实例变量的列表

PHP if else - 正确使用 "else"

php - 如何将php数组保存到mysql表中?

php - 在 php/mysqli 中使用存储过程检索多个结果集