Yii Framework Forum: New class autoloader design - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

New class autoloader design

#1 User is offline   qiang 

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

Posted 18 July 2011 - 02:18 PM

This is the design for the new class autoloader. We have considered peer designs and the prevailing protocols.

Upon seeing a new unknown class, the class autoloader will do one of the following:
  • Include the class file declared in YiiBase::$classMap;
  • Convert namespaced class name into path alias and then include the corresponding file. For example, yii\base\Component will be converted to @yii/base/Component.php
  • Convert PEAR-styled class name into path alias and then include the corresponding file. For example, PHPUnit_Framework_TestCase will be converted to to @PHPUnit/Framework/TestCase
  • Search in the directories declared by YiiBase::$classPath (similar to php include_path) and include "$className.php", if any.
In case 1-4 all fail, the autoloader will return false, indicating the next class autoloader should take care of it.

In the above, a new format of path alias (e.g. @yii/base/Component.php) is introduced. I will describe it in a separate post.

Your comments and suggests are welcome. Thanks.
1

#2 User is offline   mindplay 

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

Posted 20 July 2011 - 11:00 AM

Seems like a pretty standard approach, from what I've seen of auto-loaders with namespace support - looks sound.

In my opinion, you can (and should) eliminate most "search" operations - in my experience, introducing namespace support makes it possible to largely eliminate searching, except when working with some legacy libraries. Searching is expensive - and attempting to optimize searches generally just ends up leading to more overhead, so it's better to eliminate it for the most part.

I don't see it mentioned here, but mapping a root-namespace to a specific folder seems like a natural feature? So that you can place third-party libraries in a folder (or a pharchive), and classes in that namespace (or any namespace below it) can be mapped directly, without any searching. For most newer third-party codebases making use of namespaces, this will most likely be the approach that most people will prefer...

(come to think of it, it would be great if Yii itself could be deployed as a pharchive - but maybe that's bleeding over into a different discussion...)
0

#3 User is offline   qiang 

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

Posted 20 July 2011 - 11:20 AM

For namespaced and PEAR-styled class names, no searching will be performed. The autoloader will simply return false if the corresponding class file doesn't exist (based on the path alias).

Path alias is described in a separate thread: http://www.yiiframew...-of-path-alias/
It absorbed your earlier suggestions about making it more generic and support URLs.

Because Yii 2.0 will use namespace for its core classes, we no longer need the lengthy class map as seen in YiiBase Yii 1.x. In the application code, a reference to unkown \yii\base\Component will trigger autoloader and load the corresponding class automatically. No need to include or import it explicitly.

We still support searching for non-namespaced classes because we expect they may still be liked by many developers in application code.
0

#4 User is offline   mindplay 

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

Posted 21 July 2011 - 08:17 AM

By the way, how come you use namespaces with lowercase initial?

Most newer PHP projects using namespaces seem to use capitalized names for namespaces - I don't think there's any official prescription for this, but the community seems to prefer capitalized namespaces, e.g. in Symfony2, Doctrine, etc. Also, "namespace" prefixes are capitalized in PEAR, and the new built-in PHP namespace (after a recent vote on php-internals) will be "PHP", not "php".
0

#5 User is offline   samdark 

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

Posted 21 July 2011 - 08:26 AM

mindplay
Can you give a link to this built-in namespace discussion? Thanks.
Yii 1.1 Application Development Cookbook

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

#6 User is offline   qiang 

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

Posted 21 July 2011 - 08:32 AM

Not sure about this. At least Lithium is using lowercase as well.

I think the main reason for these projects to use uppercase is because they inherit more or less the naming convention of PEAR.

The main benefit of using camel case is that it is easier to read when there are multiple words.
The main drawback is that the typing takes a bit more effort, and sometimes it may not be very consistent (e.g. PHP vs. Php). Also, we often use single word in namespaces.
0

#7 User is offline   mindplay 

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

Posted 21 July 2011 - 01:28 PM

View Postsamdark, on 21 July 2011 - 08:26 AM, said:

mindplay
Can you give a link to this built-in namespace discussion? Thanks.


Here: PHP 5.4 votes
1

#8 User is offline   mindplay 

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

Posted 21 July 2011 - 01:33 PM

View Postqiang, on 21 July 2011 - 08:32 AM, said:

Not sure about this. At least Lithium is using lowercase as well.

I think the main reason for these projects to use uppercase is because they inherit more or less the naming convention of PEAR.

The main benefit of using camel case is that it is easier to read when there are multiple words.
The main drawback is that the typing takes a bit more effort, and sometimes it may not be very consistent (e.g. PHP vs. Php). Also, we often use single word in namespaces.


All those good arguments aside, the community has generally settled on capitalized namespaces. Zend is another big framework that uses capitalized namespaces.

A few projects use lower-case, but most of the codebases you come across in the wild use capitalized.

See this discussion.

Edit: I think part of the thinking behind capitalized namespaces, is that they are names, and names should be capitalized, e.g. "Yii", not "yii"... I personally prefer "Rasmus" over "rasmus" ;-)
0

#9 User is offline   samdark 

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

Posted 21 July 2011 - 01:35 PM

Well, votes are nearly 50/50.
Yii 1.1 Application Development Cookbook

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

#10 User is offline   mindplay 

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

Posted 21 July 2011 - 01:40 PM

View Postsamdark, on 21 July 2011 - 01:35 PM, said:

Well, votes are nearly 50/50.


That's thinking like an American politician ;-)
0

#11 User is offline   qiang 

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

Posted 21 July 2011 - 02:08 PM

View Postmindplay, on 21 July 2011 - 01:33 PM, said:

All those good arguments aside, the community has generally settled on capitalized namespaces. Zend is another big framework that uses capitalized namespaces.

A few projects use lower-case, but most of the codebases you come across in the wild use capitalized.

See this discussion.

Edit: I think part of the thinking behind capitalized namespaces, is that they are names, and names should be capitalized, e.g. "Yii", not "yii"... I personally prefer "Rasmus" over "rasmus" ;-)


What benefits do you see? I see several drawbacks as I mentioned.

Also, it's hard to say the PHP world has settled down with capitalized namespace, even though ZF and Symfony are using that. Zend is using capitalized namespace because its 1.x version follows PEAR naming style. And Symfony2 is following Zend.

In Java and Python worlds, lower-case namespace is clearly prevailing.
0

#12 User is offline   robregonm 

  • Expert Yii Developer
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 594
  • Joined: 30-July 09
  • Location:Colombia

Posted 21 July 2011 - 03:08 PM

MyYiiNamespace vs myYiiNamespace vs myyiinamespace vs my_yii_name_space
I prefer "MyYiiNamespace", this also applies for all classes.
Ricardo Obregón

YiiFramework en Español - http://yiiframework.co/ - http://yiiframeworkenespanol.org/ - Yii Code Generator for Bootstrap
http://obregon.co/ - https://1server.co/
PHP 5.5+, nginx 1.7, MySQL(MariaDB & PerconaDB), PostgreSQL 9, Yii 2, CanJS
Follow me: @robregonm & @obregonco & @1ServerCo.
0

#13 User is offline   qiang 

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

Posted 21 July 2011 - 03:09 PM

@robregonm: it's not common to see multiple words in namespace...
0

#14 User is offline   mindplay 

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

Posted 22 July 2011 - 08:21 AM

View Postqiang, on 21 July 2011 - 02:08 PM, said:

What benefits do you see? I see several drawbacks as I mentioned.

Also, it's hard to say the PHP world has settled down with capitalized namespace, even though ZF and Symfony are using that. Zend is using capitalized namespace because its 1.x version follows PEAR naming style. And Symfony2 is following Zend.

In Java and Python worlds, lower-case namespace is clearly prevailing.


Regardless of what other languages are doing, the majority of the PHP community clearly has settled on capitalized namespaces. So I think the greatest benefit is that of consistency. If you pick lower-case namespaces, you're going to have lots of third-party packages with capitalized namespaces, and it's going to end up looking messy.

Other than that, just as mentioned, the fact that they are names in the most general sense, so "Rasmus\Schultz\Programmer" makes a lot more sense than "rasmus\schultz\Programmer". Which also just looks kind of odd to me as it's all mixed case, but that's a matter of taste, I suppose. It also makes it look to me as if the namespace is somehow less "important" than the class-name, when probably the opposite is actually the case, i.e. a machine is greater than the sum of it's components - well, hopefully ;-)

You could argue for one or the other, I guess - like the ever-undying debate about plural vs singular table-names. In the end, it's probably a matter of personal preference. Although as stated, given the fact that most new open-source projects use capitalized namespace names, I would stick with that, for consistency.

Why don't we just take a vote on it?
0

#15 User is offline   grigori 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 06-February 11

Posted 16 August 2011 - 02:41 PM

There's nothing bad in the "class map as seen in YiiBase". Moreover, I'd prefer a set of aliases for the most common names.

A very important aspect is code completion. If it works well in both Eclipse and NetBeans, case will not matter.

Why do you want to support pear-style names? How do you plan it to be used?
0

#16 User is offline   mindplay 

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

Posted 01 September 2011 - 06:26 PM

View Postgrigori, on 16 August 2011 - 02:41 PM, said:

Why do you want to support pear-style names? How do you plan it to be used?


One simple reason: people use PEAR classes.

I don't think anyone would encourage using PEAR naming conventions or support them for any other reason - the conventions made obsolete with the introduction of namespaces, but PEAR is still a large codebase of useful components.
0

#17 User is offline   grigori 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 06-February 11

Posted 01 September 2011 - 06:47 PM

View Postmindplay, on 01 September 2011 - 06:26 PM, said:

One simple reason: people use PEAR classes.


Not that many people. Much more people use Joomla, but the ratings by lemmings quantity is a bad ground for framework design.
0

#18 User is offline   mindplay 

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

Posted 20 September 2011 - 07:39 PM

View Postgrigori, on 01 September 2011 - 06:47 PM, said:

Not that many people. Much more people use Joomla, but the ratings by lemmings quantity is a bad ground for framework design.


I'm not going to say PEAR classes are my first choice, but there are many working and proven components in the PEAR repository - sometimes it's either that or write your own solution from scratch.
0

#19 User is offline   CGeorge 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 27-May 10
  • Location:Spain

Posted 07 October 2011 - 01:09 AM

In addition to the Yii's Autoloader, I would like to have a class similar to this one. It would make easier to use some third party libraries.
0

#20 User is offline   Lukas Kahwe Smith 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 05-November 11

Posted 08 November 2011 - 11:25 AM

you probably want to follow PSR-0

https://github.com/p...cepted/PSR-0.md
0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • 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