Yii Framework Forum: Component life cycles - Yii Framework Forum

Jump to content

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

Component life cycles

#1 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,892
  • Joined: 04-October 08
  • Location:DC, USA

Posted 28 July 2011 - 01:20 PM

In 1.x, several classes have a method named init() which is called after constructor and property initialization.

In 2.0, I am thinking to put this into the base Component class and by default init() will call behaviors() to register declared behaviors.
Accordingly, Yii::createComponent() will be modified to call init() after creating a new component.

What do you feel about this change?

Pros:
- more unified than 1.x: every component will have init(), not just app components, models.
- behavior registration is more standardized. In 1.x, some components may not attach behaviors unless you explicitly attach them.

Cons:
- performance degradation: now every component instantiation will have an additional call to init(). This may not be a big issue, however.
- every component will have a method named 'init()', which is probably too common and may conflict with the similar needs in child classes. Perhaps this is not a real issue either, though.
1

#2 User is offline   samdark 

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

Posted 28 July 2011 - 03:50 PM

What's currently done via component?

1. getter/setter as property. That's a good and a bad feature of the framework at the same time. Produces short good looking syntax but adds some confusion. Personally I like it but can live with explicit getters/setters.

2. Events. If not requiring describing events explicitly, events support can be done in a separate class. We'll be able to use it for anything, not only components.

3. Behaviors. PHP 5.4 will support this feature natively (traits are actually implemented as state-aware) but we're building Yii2 with 5.3 in mind. Feature is both good and bad again: gives a good amount of flexibility but also adds magic-like features. The only popular place where behaviors are currently used is models (am I wrong about models-only?).

So there are pros and cons. Should we use base component class at all?
Yii 1.1 Application Development Cookbook

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

#3 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,892
  • Joined: 04-October 08
  • Location:DC, USA

Posted 28 July 2011 - 04:18 PM

Our general rule for Yii 2.0 is that we will not make significant changes to proven designs. Otherwise, we are throwing ourselves into unknown space. For this reason, we will keep most of the features in our current foundational classes, and we will have a base component class like in Yii 1.x.

1. getter/setter: can't think of what's wrong with it. IMO, this should be natively supported by PHP.

2. We have discussed about this. I think we should limit our scope for now, not trying to support everything unless we see real needs for that.

3. As you can see behavior is another feature that should be supported at language level. Since Yii 2.0 will be based on 5.3 instead of 5.4, I think we should support it at the base component level. Personally, I never like behaviors as I prefer to explicit programming.

There's no perfect code. Making a good compromise is the key.
0

#4 User is offline   samdark 

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

Posted 28 July 2011 - 04:41 PM

Got your point. Generally I agree. There's no harm using component as a base or at least I don't see it. Now about actual questions asked…

Moving init() to Component looks like a good idea. Except http://www.yiiframew...1.1/CGoogleApi/ everything that uses init currently extends CComponent. Cons aren't looking critical to me.
Yii 1.1 Application Development Cookbook

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

#5 User is offline   Gustavo 

  • Master Member
  • Yii
  • Group: Moderators
  • Posts: 916
  • Joined: 27-July 10
  • Location:Curitiba - Brasil

Posted 28 July 2011 - 06:39 PM

View Postsamdark, on 28 July 2011 - 04:41 PM, said:

Moving init() to Component looks like a good idea. ... . Cons aren't looking critical to me.


Agreed
--
Extensions:
translate modue - module to handle translations
multiActiveRecord - db selection in models
redisCache - redis cache component
mpCpanel - interact with cpanel api
mUploadify - use uploadify uploader in your application

Gustavo Salomé Silva
0

#6 User is offline   mindplay 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 397
  • Joined: 03-September 09
  • Location:New York

Posted 31 July 2011 - 06:08 PM

I have needed component initialization many times, and had to implement it myself.

I vote yes.
0

#7 User is offline   wei 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 147
  • Joined: 04-October 08

Posted 31 July 2011 - 09:31 PM

I propose passing in an Application object in the init. e.g.

function init($app)
{
$db = $app->getComponent($this->db);
}
0

#8 User is offline   samdark 

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

Posted 01 August 2011 - 09:15 AM

wei
What's the benefit? We can't have two application instances at the same time.
Yii 1.1 Application Development Cookbook

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

#9 User is offline   wei 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 147
  • Joined: 04-October 08

Posted 02 August 2011 - 09:44 AM

View Postsamdark, on 01 August 2011 - 09:15 AM, said:

wei
What's the benefit? We can't have two application instances at the same time.


To avoid Yii::app() calls in component classes. This is essentially a global variable.
0

#10 User is offline   samdark 

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

Posted 02 August 2011 - 09:49 AM

OK, but what's practical benefit, not theoretical one? We do have application instance in unit tests, we aren't going to use Yii components outside of Yii itself.
Yii 1.1 Application Development Cookbook

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

#11 User is offline   mindplay 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 397
  • Joined: 03-September 09
  • Location:New York

Posted 05 August 2011 - 06:15 AM

View Postwei, on 02 August 2011 - 09:44 AM, said:

To avoid Yii::app() calls in component classes. This is essentially a global variable.


But it isn't a variable, it's a static method that gives you the currently running Yii application.

We're getting into another discussion, but I don't like that either - anything static makes me cringe.

The only way around it though, is probably dependency injection - which is what you're suggesting, but that would mean that every class that has a dependency on an application component would need to extend CComponent. That would probably result in a lot overhead, and it raises more questions - like, if we're going to introduce dependency injection, why not introduce "real" dependency injection, that actually injects the application components you need, rather than injecting the dependency registry itself? (which is essentially what a Yii application object is)
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