所以,我在处理一些php-oo东西时遇到了麻烦。我认为该准则最能解释这一点:
class foo {
$someprop;
public function __construct($id){
$this->populate($id);
}
private function populate($id){
global $db;
// obviously not the call, but to illustrate the point:
$items = $db->get_from_var1_by_var2(get_class($this),$id);
while(list($k,$v) = each($items)){
$this->setVar($k,$v);
}
}
private function setVar($k,$v){
// filter stuff, like convert JSON to arrays and such.
$this->$k = $v;
}
}
class bar extends foo {
$otherprop;
public function __construct($id){
parent::__construct($id);
}
private function setVar($k,$v){
// different filters than parent.
$this->$k = $v;
}
}
现在,假设我的foo表中有一个prop,而我的bar表中有另一个prop,那么当我传入一个id时,应该在我的对象上设置vars。
但是,由于某种原因,foo工作得很好,但是bar没有设置任何内容。
我的假设是它在$this->setvar()调用上崩溃了,并且调用了错误的setvar,但是如果get_类($this)正在工作(它是工作的),那么$this不应该是bar,并且通过关联,setvar()应该是$bar方法吗?
有人看到我错过了什么/做错了什么吗?