Yii Framework Forum: Code preview: Object and Component - Yii Framework Forum

Jump to content

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

Code preview: Object and Component

#1 User is offline   qiang 

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

Posted 29 May 2012 - 08:26 PM

*
POPULAR

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.

https://gist.github.com/2835928
https://gist.github.com/2835927

Attached File(s)


7

#2 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 29 May 2012 - 10:26 PM

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!
0

#3 User is offline   Seal 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 126
  • Joined: 02-February 10

Posted 30 May 2012 - 02:20 AM

I am seriously excited about the namespace features. Posted Image 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.
Sylvester La-Tunje

Posted Image
0

#4 User is offline   Mike 

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

Posted 30 May 2012 - 06:56 AM

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

#5 User is offline   Suralc 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 10-January 12
  • Location:Germany

Posted 30 May 2012 - 08:28 AM

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

#6 User is offline   Da:Sourcerer 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,222
  • Joined: 30-March 11
  • Location:Berlin, Germany

Posted 30 May 2012 - 08:37 AM

Hm. How about Object::instance() instead of Object::newInstance()?
programmer /ˈprəʊgramə/, noun: a device that converts ►coffee into ►code
0

#7 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 30 May 2012 - 08:47 AM

View PostDa:Sourcerer, on 30 May 2012 - 08:37 AM, said:

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. :)
"Less noise - more signal"
0

#8 User is offline   Da:Sourcerer 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,222
  • Joined: 30-March 11
  • Location:Berlin, Germany

Posted 30 May 2012 - 09:00 AM

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?
programmer /ˈprəʊgramə/, noun: a device that converts ►coffee into ►code
0

#9 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 30 May 2012 - 10:35 AM

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.
"Less noise - more signal"
0

#10 User is offline   Da:Sourcerer 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,222
  • Joined: 30-March 11
  • Location:Berlin, Germany

Posted 30 May 2012 - 10:42 AM

Heh, indeed. Never said anything, then ;)
programmer /ˈprəʊgramə/, noun: a device that converts ►coffee into ►code
0

#11 User is offline   Seal 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 126
  • Joined: 02-February 10

Posted 30 May 2012 - 11:33 AM

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

Posted Image
0

#12 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 31 May 2012 - 04:27 PM

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

#13 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 31 May 2012 - 04:41 PM

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

#14 User is offline   qiang 

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

Posted 31 May 2012 - 05:27 PM

@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.
0

#15 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 31 May 2012 - 05:55 PM

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

#16 User is offline   intel352 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 196
  • Joined: 05-February 10
  • Location:Southport, NC

Posted 31 May 2012 - 08:03 PM

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... :-)
Need live Yii support? - Join the #yii IRC channel on Freenode!
0

#17 User is offline   Gustavo 

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

Posted 10 December 2012 - 11:32 AM

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
}

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

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