如何在PHP

时间:2017-10-03 18:05:55

标签: php class relationship

hypernym-hyponym (supertype-subtype) relations between types (classes) defining a taxonomic hierarchy, where
    for a subsumption relation: a hyponym (subtype, subclass) has a type-of (is-a) relationship with its hypernym (supertype, superclass);
holonym-meronym (whole/entity/container-part/constituent/member) relations between types (classes) defining a possessive hierarchy, where
    for an aggregation (i.e. without ownership) relation:
        a holonym (whole) has a has-a relationship with its meronym (part),
    for a composition (i.e. with ownership) relation:
        a meronym (constituent) has a part-of relationship with its holonym (entity),
    for a containment[1] relation:
        a meronym (member) has a member-of relationship with its holonym (container);
concept-object (type-token) relations between types (classes) and objects (instances), where
    a token (object) has an instance-of relationship with its type (class).

上面引用了关于Is-a的Wiki文章和OOP中类之间的has-a关系。

我希望看到使用PHP语法引用的每种关系类型的示例。如果我对此是正确的,请告诉我。

首先超类型 - 子类型

abstract class User {...}

class Student extends User {...}
class Employee extends User {...}

第二个整个/实体/容器 - 部分/成员/成员让我感到困惑。

聚合是通过依赖注入在PHP中完成的吗?

   class Car_Wheel {...} 

   $wheel = new Car_Wheel();

   class Car {

      private $wheel;

      public __construct($wheel) {
         $this->wheel = $wheel;   
      }
   }

   $Car = new Car($wheel);
在php trough traitsincludes中实现

合成

trait Car_Wheel {...}

class Car {

   use Car_Wheel;

   ...
}

//file `car_wheel.php`

...
private function car_wheel_rotate(...) {};
...

//file `car.php`

class Car {} {

   include_once "car_wheel.php";

   ...
}
在php中通过实例化对象内的对象来实现

遏制

class Car_Wheel {...} 
class Car_Seat {...}

class Car {
   private $wheel;
   private $seat;

   public __construct() {
      $this->wheel = new Car_Wheel();   
      $this->seat = new Car_Seat();   
   }   
}

第三个是类型令牌也很简单

class Database {...}

$DB = new Database();

0 个答案:

没有答案
相关问题