Yii Framework Forum: Why "on the fly" object property assignement/creation doesn't work? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Why "on the fly" object property assignement/creation doesn't work? Rate Topic: -----

#1 User is offline   skyblaze 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 106
  • Joined: 07-January 09

Posted 05 January 2010 - 12:08 PM

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';
}
0

#2 User is offline   PoL 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 506
  • Joined: 05-November 08
  • Location:Buenos Aires, Argentina

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!
0

#3 User is offline   skyblaze 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 106
  • Joined: 07-January 09

Posted 05 January 2010 - 09:19 PM

View PostPoL, on 05 January 2010 - 12:44 PM, said:

Cause you need to define it!
Class YOurCOntroller extend CController{
   public headTitle;

   //your functions.

}


but we all know that we can do that without predeclaring it. It is valid php code
0

#4 User is offline   jayrulez 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 348
  • Joined: 29-July 09

Posted 05 January 2010 - 10:13 PM

The php magic method __set() was overridden by CComponent::__set() as follows

	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.
1

#5 User is offline   skyblaze 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 106
  • Joined: 07-January 09

Posted 06 January 2010 - 06:21 AM

View Postjayrulez, on 05 January 2010 - 10:13 PM, said:

The php magic method __set() was overridden by CComponent::__set() as follows

	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
0

#6 User is offline   skyblaze 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 106
  • Joined: 07-January 09

Posted 06 January 2010 - 06:22 AM

View Postskyblaze, 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.
0

#7 User is offline   jayrulez 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 348
  • Joined: 29-July 09

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users