Yii Framework Forum: Magic Methods And Events/behaviors - Yii Framework Forum

Jump to content

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

Magic Methods And Events/behaviors Why the extra space?

#1 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 16 May 2013 - 02:55 AM

Looking at the source of Component.php i found this code in the magic __set():


	public function __set($name, $value)
	{
		$setter = 'set' . $name;
		if (method_exists($this, $setter)) {
			// set property
			$this->$setter($value);
			return;
		} elseif (strncmp($name, 'on ', 3) === 0) {
			// on event: attach event handler
			$this->on(trim(substr($name, 3)), $value);
			return;
		} elseif (strncmp($name, 'as ', 3) === 0) {
			// as behavior: attach behavior
			$name = trim(substr($name, 3));
			$this->attachBehavior($name, $value instanceof Behavior ? $value : Yii::createObject($value));
			return;



That basically means because of the extra space we have to use it like this:

$component->{'on something'} = function() ...


I wonder what was the backround of this decision. Why isn't it simply $component->onSomething = ...?
0

#2 User is offline   Fragoulas 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 11-July 11

Posted 16 May 2013 - 03:26 AM

Actually (I think) it will be like this:

$component->on('something', function() { ... });


the 'on something' way is for when you want to do something like:

$this->widget('somewidget', array(
    'on something' => function() {...},
    'on somethingElse' => function() {...},
));


etc.

I am not 100% this is correct tho :)
0

#3 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,745
  • Joined: 17-January 09
  • Location:Russia

Posted 16 May 2013 - 05:18 AM

https://github.com/y...yii2/issues/157
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#4 User is offline   Rodrigo Coelho 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 665
  • Joined: 05-August 10
  • Location:Rio de Janeiro, Brazil

Posted 16 May 2013 - 05:19 AM

Fragoulas is correct. See Qiang's post here.

I didn't check the code yet, but it appears that it is handed over to __set to discover and attach the handler.
0

#5 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 16 May 2013 - 05:42 AM

I see now. Thanks!
0

#6 User is offline   Raoul 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 645
  • Joined: 29-November 08
  • Location:Paris, France

Posted 16 May 2013 - 08:01 AM

Relying on a single tiny space character between 'on' and 'someting' seems to me a potential source of suicide among Yii2 developers (in particular after 2 am)

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