Code preview: Object and Component

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.

EDIT: same code on gist.

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:

  1. the new way using Classname::newInstance()

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

Good job!

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.

Overall the design more flexible.

Looking at line 92 of Component.php it seems we can also attach behaviors through __set()? Is this missing from the documentation above this method?

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.

Hm. How about Object::instance() instead of Object::newInstance()?

That’s not a good idea. What does ‘instance’ do? Return an instance or create one? Or both? ‘newInstance’ is much clearer IMO.

Great preview.

I like the coding conventions which align with what I use myself. :)

Well, Object::factory() were fine by me, too. It’s really just a matter of personal preferences and individual ideas of coding aesthetics ;)

Another note: It’s clearly not too late to rename yii\base\Initable to yii\base\Initiable, isn’t it?

Huh?

Very few libraries uses ‘initiable’ - because there’s no reason to. ‘initable’ is fine.

It means ‘able to init’.

If the related function was called ‘initiate’, it would have been prudent.

But it’s called init.

And of course ‘init’ is not in your standard dictionary.

Heh, indeed. Never said anything, then ;)

I must say that when I saw initable, I did think why not "initable" but thought this was a trivial issue.

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.

Another issue with behaviors:

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.

@onman: Good catch. Fixed. Thanks! We still have Yii::createObject() which allows you specify a class using path alias. That’s the main difference.

@Mike: Fixed. Thanks!

@Suralc: Yes, we plan to introduce an exception hierarchy.

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.

I’m excited at the new direction for Yii 2.0, and the base classes remind me a bit of Lithium (aka Li3).

Can’t wait to start building projects on the new codebase… :slight_smile:

the trigger method should let the caller know if the event chain was successful handled or not.

like this

https://gist.github…ponent-php-L331

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

}