php OOP中的一个奇怪问题
嘿,伙计们。我为一些数据设计了一个模型,希望它能像这样工作:php OOP中的一个奇怪问题,php,oop,Php,Oop,嘿,伙计们。我为一些数据设计了一个模型,希望它能像这样工作:$this->groupmodel->VARIABLE->FUNCTION(VAR1,VAR2)调用函数,其中变量可更改为任何值,并传递给函数 这感觉更正确(然后说$This->groupmodel->FUNCTION(VARIABLE,VAR1,VAR2)),因为每个变量都有完全相同的函数,而且函数(技术上)是在变量上执行的。这可能吗? 请注意,VARIABLE可以设置在任何位置(在它自己的函数或被调用的函数中)(它在整个类中都是持久
$this->groupmodel->VARIABLE->FUNCTION(VAR1,VAR2)
调用函数,其中变量可更改为任何值,并传递给函数
这感觉更正确(然后说$This->groupmodel->FUNCTION(VARIABLE,VAR1,VAR2)
),因为每个变量
都有完全相同的函数,而且函数(技术上)是在变量
上执行的。这可能吗?请注意,
VARIABLE
可以设置在任何位置(在它自己的函数或被调用的函数中)(它在整个类中都是持久的,但每次调用都需要设置)
Max您应该创建一个实现您想要使用的函数的类,并且您的所有“变量”都应该是该类的对象。例如:
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对象的属性时自动创建类Kid的对象。(这就是神奇的方法_set()的作用)
事实上,它将它们创建为私有数组的元素,而不是GroupModel的真实属性
然后,当尝试访问这些“属性”时,将调用_get(),它将检索数组的元素并返回它
因为它将是Kid类的对象,所以可以调用它实现的每个方法(比如birthday())
有关重载和神奇方法(如_get和_set)的更多信息,请参阅:
您应该创建一个实现您想要使用的函数的类,并且您的所有“变量”都应该是该类的对象。例如:
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对象的属性时自动创建类Kid的对象。(这就是神奇的方法_set()的作用)
事实上,它将它们创建为私有数组的元素,而不是GroupModel的真实属性
然后,当尝试访问这些“属性”时,将调用_get(),它将检索数组的元素并返回它
因为它将是Kid类的对象,所以可以调用它实现的每个方法(比如birthday())
有关重载和神奇方法(如_get和_set)的更多信息,请参阅:
PHP5.3让您可以使用非常好的东西 假设您有两个类:Foo,它扩展了groupmodel:
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,另一件重要的事情是设置一个PHP5.3,让您使用非常好的东西 假设您有两个类:Foo,它扩展了groupmodel:
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,另一件重要的事情是设置一个是的,这是可能的。php允许您为成员和函数访问使用变量。例如:
$this->groupmodel->$myvar->myfunc($var1,$var2)代码>
这将调用$This->groupmodel->{myvar中存储的任何字符串}
请注意,如果要执行此操作,必须在类中设置groupmodel,$myvar必须是groupmodel
中的公共成员,$myvar的内容必须是有效成员,该成员也是实现myfunc()
的类。这是一个耦合的依赖关系(因此zerkms轻视这种方法)。知道你想做什么会有帮助。是的,这是可能的。php允许您为成员和函数访问使用变量。例如:$this->groupmodel->$myvar->myfunc($var1,$var2)代码>
这将调用$This->groupmodel->{myvar中存储的任何字符串}
请注意,如果要执行此操作,必须在类中设置groupmodel,$myvar必须是groupmodel
中的公共成员,$myvar的内容必须是有效成员,该成员也是实现myfunc()
的类。这是一个耦合的依赖关系(因此zerkms轻视这种方法)。这将有助于了解您正在尝试做什么。这不是一种面向对象的操作,而是试图使用对象来做可怕的事情。OOP是为了降低程序的复杂性,使程序更清晰、易懂和可读而发明的一种范例。事实上,Mazzzz发布的示例在某种程度上类似于某些语言实现OOP的方法。被调用的对象被自动称为$this,仅此而已。我喜欢那些不知道我的程序函数如何运行的人会对应该如何运行得出如此高的结论。这不是OOP,而是试图用对象做可怕的事情。OOP是为了降低程序的复杂性,使程序更清晰、易懂和可读而发明的一种范例。事实上,Mazzzz发布的示例在某种程度上类似于某些语言实现OOP的方法。被调用的对象被自动称为$this,仅此而已。我喜欢那些不知道我的程序函数如何实现的人会对如何实现它得出如此高的结论。在你的示例中,你能更详细地描述一下吗?如果不清除,则变量
之后的每个函数对于每个变量
都是完全相同的。我不知道如何调整您的解决方案来做到这一点。在您的示例中,您能更详细地描述一下吗?如果不清除,则变量之后的每个函数对于每个变量都是完全相同的