繁体   English   中英

一个奇怪的问题 php OOP

[英]A odd question php OOP

大家好。 我正在为一些数据设计一个 model,并希望它像这样工作: $this->groupmodel->VARIABLE->FUNCTION(VAR1, VAR2); 调用 function,其中 VARIABLE 可以更改为任何内容,并传递给 function。

这感觉更正确(然后说$this->groupmodel->FUNCTION(VARIABLE, VAR1, VAR2) ),因为每个VARIABLE具有完全相同的功能,并且这些功能正在(技术上)在VARIABLE上执行。 这可能吗?
请注意, VARIABLE可以在任何地方设置(在它自己的 function 或在被调用的 function 中)(它在整个 class 中持续存在,但需要设置)。

最大限度

您应该创建一个 class 来实现您要使用的功能,并且您的所有“变量”都应该是该 class 的对象。 例如:

class Kid {
    private $age = 0;
    public function _construct($age){
      $this->age = $age;
    }
    public function birthday() { // implement in Kid instead of in Groupmodel
      $this->age++;
      echo "Growing old... ";
    }
    public function age($age_new = null){  // age setter and getter
      if(!is_null($age_new)){
        $this->age = $age_new;
      }
      return $this->age;
    }
}

然后在您的 groupmodel 中:

class GroupModel {

  private $variables;

  public function _set($name, $value) {
    if (array_key_exists($name, $this->variables)) {
      $this->variables[$name]->age($value);
    } else {
      $this->variables[$name] = new Kid($value);
    }
  }

  public function _get($name) {
    if (array_key_exists($name, $this->variables)) {
      return $this->variables[$name];
    } else {
      return null;
    }
  }
}

所以你可以打电话:

$this->groupmodel = new GroupModel()
$this->groupmodel->var1 = 8
$this->groupmodel->var1->birthday();  // will add 1 to var1's age and print "Growing old"
$this->groupmodel->var1 = 9  // will replace var1's age

我们在这里所做的是在每次尝试设置 GroupModel object 的属性时自动创建 class Kid 的对象。 (这就是魔法方法 _set() 所做的)

事实上,它将它们创建为私有数组的元素,而不是 GroupModel 的真实属性。

然后,当尝试访问这些“属性”时,_get() 将被调用,它将检索数组的元素并返回它。

由于它将是 class Kid 的 object,因此您可以调用它实现的每个方法(如生日())。

有关重载和魔术方法(如 _get 和 _set)的更多信息,请参阅:

http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members

PHP 5.3 让您使用非常好的东西,称为后期 static 绑定

假设您有 2 个类:扩展 groupmodel 的 Foo:

class groupmodel { 
      const MY_CONST = 'groupmodel'; 

      protected function myName(){
         echo static::MY_CONST; //Will print 'groupmodel'; 
      }


      protected function whoAmI(){ 
        //do something here
      }
}

和福:

class Foo extends groupmodel { 
      const MY_CONST = 'ClassFoo'; 

      public function tellMyName(){ 
        $this->myName(); //Will print 'ClassFoo';
      }
} 

实际上,这个想法不是使用

$this->groupmodel->FUNCTION(VARIABLE, VAR1, VAR2)
OR 
$this->groupmodel->VARIABLE->FUNCTION(VAR1, VAR2);

你将使用:

$object = new Foo(); 
$object->tellMyName(); //Will print 'ClassFoo'

现在 $object 将授予所有groupmodel方法。

与您的案例以及尽可能多地使用 OOP 一起工作的另一件重要事情是设置抽象 class

对的,这是可能的。 php 允许您对成员和 function 访问使用变量。 例如: $this->groupmodel->$myvar->myfunc($var1, $var2);

这将调用$this->groupmodel->{Whatever-string-is-stored-in-myvar}

请注意,如果要执行此操作,必须在 class 中设置 groupmodel 并且 $myvar 必须是groupmodel中的公共成员,并且 $myvar 的内容必须是有效成员,并且也是实现myfunc()的 class 。 这是很多耦合依赖(因此 zerkms 贬低这种方法)。 不过,这将有助于了解您要做什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM