New class autoloader design

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:

[list=1][]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.[/list]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.

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

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.yiiframework.com/forum/index.php?/topic/21688-design-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.

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

mindplay

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

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.

Here: PHP 5.4 votes

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" :wink:

Well, votes are nearly 50/50.

That’s thinking like an American politician :wink:

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.

MyYiiNamespace vs myYiiNamespace vs myyiinamespace vs my_yii_name_space

I prefer "MyYiiNamespace", this also applies for all classes.

@robregonm: it’s not common to see multiple words in namespace…

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

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?

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?

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.

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.

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.

you probably want to follow PSR-0