I’d like to share with you the most important classes in Yii 2.0: Object and Class. They provide the foundation for all other advanced features of Yii. They are close to release quality, but your suggestions and feedback are always welcome. The code should also show the new coding style that is used throughout the whole 2.0 codebase.
The new coding style with the many extra spaces looks more to how I style my code, so I enjoy that.
Just like I’m going to use the static attribute Yii::$objectConfig. A lot.
I still need to get used to the new class names: I kinda miss the initial ‘C’ on classnames and ‘I’ on interface names (got so used to them, but I’ll hide my tears).
I noticed 2 ways to create a new instance:
the new way using Classname::newInstance()
the old way using Yii::createObject()
Will both of these remain? Or are they different?
Finally I think the description of the Component.__get() method is not quite the same as what the code does: the description says a behavior object may be returned, but I don’t find that back in the code.
Overall I like the new style and can’t wait to get used to the new and improved gadgets.
I am seriously excited about the namespace features. My class names were getting too long.
Abstracting the common magic methods into a further object base class is a great idea and a feature that I welcome very much as it makes it much easier to write new lean classes.
$model = Foo::newInstance(array('c' => 3), 1, 2);
Taking advantage of the ReflectionClass, I find the the newInstance() more intuitive.
Component L.207 wouldn’t a BadMethodCallException (part of SPL) be more appropiate here?
Also all-lowercase namespace? Its just athetic, but “Yii\Bla” just looks better as “yii\bla” in my oppinion. (I know i can use the first version in my code, but yeah .
Markdown(If I’m not mistaken) as documentation syntax, interesting.
I was just playing around with your new Component class and noticed some (possibly unwanted) behavior:
The detachBehaviors() method does not only remove all behaviors but also sets Component->_b to null.
Next time you call a non-existing method on the class (the __call(…) method is then called) which may in turn call ensureBehaviors(). This will re-install the class behaviors as Component->_b is null.
My point of view:
detachBehaviors() should detach all behaviors leaving Component->_b an empty array.
a new method resetBehaviors() should detach all behaviors and set Component->_b to null.
detachBehavior(…) should first call ensureBehaviors().
In addition to this: if you’ll make the change of my reply directly above then detachBehaviors() should initialise Component->_b to array() if it was still null.
when attaching a behavior through the __set() method it always seems to expect a value that is passed to Yii::createObject(). But this value can already be a behavior object and should then be used as is.
If I want to interrupt the event for some reason, like if there is no permission or something, I would set $event->handled to true or to be easier just return false and it would return false in trigger method, allowing the caller to know whether the event was successful.
if( $this->trigger('beforeFind', $myEvent) ){
// do something
}