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);
to call a function, where VARIABLE is changeable to anything, and passed to the 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
. Is this possible?
VARIABLE
can be set anywhere (in its own function or in the function being called) 开发者_JAVA技巧(it is persistent throughout the class, but needs to be set each call).
Max
You should create a class implementing the functions you want to use, and all your "variables" should be objects of that 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:
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. (This is what the magic method _set() does)
In fact it creates them as elements of a private array instead of real properties of GroupModel.
Then, when trying to access those "properties", _get() will be invoked and it will retrieve the element of the array and return it.
As it will be an object of class Kid, you could call every method it implements (like birthday()).
For more information on Overloading and magic methods like _get and _set, see:
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
Lets say you have 2 classes: Foo which extends groupmodel:
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.
another important thing with your case and for working with OOP as much as you can is setting up an abstract class
Yes it is possible. php allows you to use variables for both member and function access. For example: $this->groupmodel->$myvar->myfunc($var1, $var2);
This will call $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()
. This is a lot of coupled dependency (hence zerkms' disparaging of this approach). Would help to know what you're trying to do, though.
精彩评论