将类实例分配给另一个类的静态成员变量

时间:2020-02-01 01:28:13

标签: php oop

首先,我认为python太疯狂了,但看起来php就是狂野的西部。

<?php
use Creations\Robot;

class RoboticAnimal extends Robot {
  use Creations\AnimalMixin;

  public function __construct ($speed, $name, $band, $wheels, $age, $weight, $blood_type) {
    parent::__construct($name, $band, $wheels);
    $this->__constructAnimal(now(), $age, $weight, $blood_type);
    $this->speed = $speed;
  }

  // !! Don't forget to override these on you object that extends RoboticAnimal !!
  // There should be a preprocces step to allow this:
  //   public static $foo = new AnimalTraits("Slug", 69, 420, 9001);
  public static $foo = [
     "AnimalType": "Slug",
     "MaxAmount": 69,
     "MatingSpeed": 420,
     "PowerLevel": 9001
  ];

  public static $robotTraits = [ "Immortal": true ]; 

  ...
}

所有动物都需要具有这四个参数AnimalType MaxAmount MatingSpeed PowerLevel。因此,为了管理这4个值,我们创建了AnimalTraits对象,因为OOP很好,但是我们不能将这些动物性状实例分配给我们的类,如代码片段(和注释)所示。

是否可以将类的对象实例分配给其他类的静态成员字段? (请参见代码中静态成员的注释) 考虑到我们分配数组,这似乎应该可行,这就是 OOP Way

0 个答案:

没有答案