简体   繁体   English

一个奇怪的问题 php OOP

[英]A odd question php OOP

Hey guys.大家好。 I was designing a model for some data, and wanted it to work like: $this->groupmodel->VARIABLE->FUNCTION(VAR1, VAR2);我正在为一些数据设计一个 model,并希望它像这样工作: $this->groupmodel->VARIABLE->FUNCTION(VAR1, VAR2); to call a function, where VARIABLE is changeable to anything, and passed to the function.调用 function,其中 VARIABLE 可以更改为任何内容,并传递给 function。

This feels more correct (then say $this->groupmodel->FUNCTION(VARIABLE, VAR1, VAR2) ), because each VARIABLE has the exact same functions, and the functions are being preformed (technically) on VARIABLE .这感觉更正确(然后说$this->groupmodel->FUNCTION(VARIABLE, VAR1, VAR2) ),因为每个VARIABLE具有完全相同的功能,并且这些功能正在(技术上)在VARIABLE上执行。 Is this possible?这可能吗?
Note that VARIABLE can be set anywhere (in its own function or in the function being called) (it is persistent throughout the class, but needs to be set each call).请注意, VARIABLE可以在任何地方设置(在它自己的 function 或在被调用的 function 中)(它在整个 class 中持续存在,但需要设置)。

Max最大限度

You should create a class implementing the functions you want to use, and all your "variables" should be objects of that class.您应该创建一个 class 来实现您要使用的功能,并且您的所有“变量”都应该是该 class 的对象。 For instance:例如:

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;
    }
}

And then inside your groupmodel:然后在您的 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;
    }
  }
}

so you can call:所以你可以打电话:

$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

What we are doing here is creating objects of class Kid automatically every time you try to set a property of the GroupModel object.我们在这里所做的是在每次尝试设置 GroupModel object 的属性时自动创建 class Kid 的对象。 (This is what the magic method _set() does) (这就是魔法方法 _set() 所做的)

In fact it creates them as elements of a private array instead of real properties of GroupModel.事实上,它将它们创建为私有数组的元素,而不是 GroupModel 的真实属性。

Then, when trying to access those "properties", _get() will be invoked and it will retrieve the element of the array and return it.然后,当尝试访问这些“属性”时,_get() 将被调用,它将检索数组的元素并返回它。

As it will be an object of class Kid, you could call every method it implements (like birthday()).由于它将是 class Kid 的 object,因此您可以调用它实现的每个方法(如生日())。

For more information on Overloading and magic methods like _get and _set, see:有关重载和魔术方法(如 _get 和 _set)的更多信息,请参阅:

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

PHP 5.3 let you use very nice thing called late static bindings PHP 5.3 让您使用非常好的东西,称为后期 static 绑定

Lets say you have 2 classes: Foo which extends groupmodel:假设您有 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
      }
}

and Foo:和福:

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

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

Actually, the idea is instead of using实际上,这个想法不是使用

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

you will use:你将使用:

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

and now $object will grant all of groupmodel methods.现在 $object 将授予所有groupmodel方法。

another important thing with your case and for working with OOP as much as you can is setting up an abstract class与您的案例以及尽可能多地使用 OOP 一起工作的另一件重要事情是设置抽象 class

Yes it is possible.对的,这是可能的。 php allows you to use variables for both member and function access. php 允许您对成员和 function 访问使用变量。 For example: $this->groupmodel->$myvar->myfunc($var1, $var2);例如: $this->groupmodel->$myvar->myfunc($var1, $var2);

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

Note that if you want to do this, groupmodel must be set in the class and $myvar must be a public member in the groupmodel and the contents of $myvar must be a valid member that is also a class that implements myfunc() .请注意,如果要执行此操作,必须在 class 中设置 groupmodel 并且 $myvar 必须是groupmodel中的公共成员,并且 $myvar 的内容必须是有效成员,并且也是实现myfunc()的 class 。 This is a lot of coupled dependency (hence zerkms' disparaging of this approach).这是很多耦合依赖(因此 zerkms 贬低这种方法)。 Would help to know what you're trying to do, though.不过,这将有助于了解您要做什么。

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

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