As the topic i would like to know why i got an exception of property not defined in class when i try in a controller:
public function actionMyAction() {
$this->headTitle = 'Area Amministrativa';
}
Page 1 of 1
Why "on the fly" object property assignement/creation doesn't work?
#2
Posted 05 January 2010 - 12:44 PM
Cause you need to define it!
Class YOurCOntroller extend CController{ public headTitle; //your functions. }
Don't say what you think, think what you say
The problem is communication! Excess of communication!
The problem is communication! Excess of communication!
#4
Posted 05 January 2010 - 10:13 PM
The php magic method __set() was overridden by CComponent::__set() as follows
If you wish to set properties "on the fly", then override the __set method of your class to allow it.
public function __set($name,$value) { $setter='set'.$name; if(method_exists($this,$setter)) return $this->$setter($value); else if(strncasecmp($name,'on',2)===0 && method_exists($this,$name)) { // duplicating getEventHandlers() here for performance $name=strtolower($name); if(!isset($this->_e[$name])) $this->_e[$name]=new CList; return $this->_e[$name]->add($value); } else if(is_array($this->_m)) { foreach($this->_m as $object) { if($object->getEnabled() && (property_exists($object,$name) || $object->canSetProperty($name))) return $object->$name=$value; } } if(method_exists($this,'get'.$name)) throw new CException(Yii::t('yii','Property "{class}.{property}" is read only.', array('{class}'=>get_class($this), '{property}'=>$name))); else throw new CException(Yii::t('yii','Property "{class}.{property}" is not defined.', array('{class}'=>get_class($this), '{property}'=>$name))); }
If you wish to set properties "on the fly", then override the __set method of your class to allow it.
php: foreach(array('cat', 'dog', 'cow') as $animal) echo $animal."\n"; python: [(animal, print(animal)) for animal in ['cat', 'dog', 'cow']] ruby: ['cat', 'dog', 'cow'].each {|animal| puts animal}
You say Tomato, I say Tomato.
#5
Posted 06 January 2010 - 06:21 AM
jayrulez, on 05 January 2010 - 10:13 PM, said:
The php magic method __set() was overridden by CComponent::__set() as follows
If you wish to set properties "on the fly", then override the __set method of your class to allow it.
public function __set($name,$value) { $setter='set'.$name; if(method_exists($this,$setter)) return $this->$setter($value); else if(strncasecmp($name,'on',2)===0 && method_exists($this,$name)) { // duplicating getEventHandlers() here for performance $name=strtolower($name); if(!isset($this->_e[$name])) $this->_e[$name]=new CList; return $this->_e[$name]->add($value); } else if(is_array($this->_m)) { foreach($this->_m as $object) { if($object->getEnabled() && (property_exists($object,$name) || $object->canSetProperty($name))) return $object->$name=$value; } } if(method_exists($this,'get'.$name)) throw new CException(Yii::t('yii','Property "{class}.{property}" is read only.', array('{class}'=>get_class($this), '{property}'=>$name))); else throw new CException(Yii::t('yii','Property "{class}.{property}" is not defined.', array('{class}'=>get_class($this), '{property}'=>$name))); }
If you wish to set properties "on the fly", then override the __set method of your class to allow it.
what i mean is that the __set method isn't necessary. It is valid to assign an object property in a method even when it is not predeclared...it is valid php code
#6
Posted 06 January 2010 - 06:22 AM
skyblaze, on 06 January 2010 - 06:21 AM, said:
what i mean is that the __set method isn't necessary. It is valid to assign an object property in a method even when it is not predeclared...it is valid php code
and then i quote this from the yii defenitive guide:
Inside the view script, we can access the controller instance using $this. We can thus pull in any property of the controller by evaluating $this->propertyName in the view.
#7
Posted 06 January 2010 - 09:50 AM
It is valid php code but it's just the preference of Yii(author) to do so. At this point I'm not sure for what reason it's done but i like that strictness.
php: foreach(array('cat', 'dog', 'cow') as $animal) echo $animal."\n"; python: [(animal, print(animal)) for animal in ['cat', 'dog', 'cow']] ruby: ['cat', 'dog', 'cow'].each {|animal| puts animal}
You say Tomato, I say Tomato.
Share this topic:
Page 1 of 1