nacesprin
(Javierleal)
June 20, 2010, 3:01pm
1
Hello. I have this overriding getter in Users.php model:
public function __get($name)
{
$getter='get'.$name;
if(method_exists($this,$getter))
return $this->$getter();
return parent::__get($name);
}
And this is the field getter for field ‘CP’
public function getCP()
{
//here need some code to alter the 'CP' field result
return $this->cp;
}
But I obtain follow error:
Description:
Undefined property: Users::$cp
Source:
C:\EasyPHP 2.0b1\www\yii\protected\models\Users.php(178)
00175:
00176: public function getCP()
00177: {
00178: return $this->cp;
00179:
00180: }
How can I get the ‘CP’ field value?
Thanks
xx666xx
(Jerryablan)
June 20, 2010, 3:20pm
2
Hello. I have this overriding getter in Users.php model:
public function __get($name)
{
$getter='get'.$name;
if(method_exists($this,$getter))
return $this->$getter();
return parent::__get($name);
}
And this is the field getter for field ‘CP’
public function getCP()
{
//here need some code to alter the 'CP' field result
return $this->cp;
}
But I obtain follow error:
Description:
Undefined property: Users::$cp
Source:
C:\EasyPHP 2.0b1\www\yii\protected\models\Users.php(178)
00175:
00176: public function getCP()
00177: {
00178: return $this->cp;
00179:
00180: }
How can I get the ‘CP’ field value?
Thanks
First off, you don’t have to override __get, CComponent already does what you are doing.
Secondly, your get method is getCP, so you need to reference it as:
$this->CP or $this->getCP()
andy_s
(Arekandrei)
June 21, 2010, 7:55am
3
Yes, you don’t have to override __get() method. You just must define “cp” in your class:
protected $cp;
And a corresponding method to get it’s value:
public function getCp()
{
return $this->cp;
}
To get cp’s value inside a class you should write: $this->getCp(). Outside a class you can use $class->cp or $class->CP or $class->getCp() because function method_exists() is case-insensitive.
mikl
(Mike)
June 21, 2010, 8:34am
4
… and you don’t need a getter at all, if you don’t need to change the value of $this->cp before returning.
nacesprin
(Javierleal)
June 21, 2010, 9:23am
5
Thanks people!.
Yes, I need to change value before return. Because of that, I have this issue