Component life cycles
Posted 28 July 2011 - 01:20 PM
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?
- 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.
- 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.
Posted 28 July 2011 - 03:50 PM
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?
Posted 28 July 2011 - 04:18 PM
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.
Posted 28 July 2011 - 04:41 PM
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.
Posted 28 July 2011 - 06:39 PM
Posted 31 July 2011 - 06:08 PM
I vote yes.
Posted 02 August 2011 - 09:49 AM
Posted 05 August 2011 - 06:15 AM
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)