Undefined property when overriding getters [SOLVED]

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()

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.

… and you don’t need a getter at all, if you don’t need to change the value of $this->cp before returning.

Thanks people!.

Yes, I need to change value before return. Because of that, I have this issue ;)