views:

313

answers:

2

I know symfony can autoload files with spl_auto_register,but is it clever enough?

If only a.php is needed to load,will it also load b.php?

A: 

The autoloader will only load the files necessary to instantiate a requested class.

blockhead
Can you prove it?
I've looked into its code and don't find such logic there.
Symfony's autoloader works by mapping class names to physical files. There isn't much logic necessary for that.
blockhead
But when we throw our own stuff right in the lib/ directory,symfony can by no means know where it is,how it will iterate all the possible places,in that case,it'll inevitably loaded unnecessary files.Is that right?
Symfony iterates over it once, and then stores the locations in a map which is cached.
blockhead
Once the map is created, it uses that to load the class you want.
blockhead
Where is the map?I don't see that map in `cache/`,AFAIK,symfony only stores a map for its core classes.
cache/*/*/config/config_autoload.yml.php
blockhead
Oh,I just checked,it only exists in **dev** environment,my version is 1.4.Is that so?How about **product** environment?
Have you run your site in *production* yet? It only gets generated when you do so. But it is certainly there.
blockhead
+1  A: 

The autoloader works by storing an array of key value pairs such that

classname => /full/path/to/class

In symfony 1.3/1.4 two cache files get created. One for the core classes, created by sfCoreAutoload, and stored in /cache/project.autoload. For the classes that are autoloaded from within your project directory, the array of key value pairs is stored in */cache/app/_env_/config/config_autoload.yml.php*. This file is generated by the config handler sfAutoloadConfigHandler.

Some useful points to remember:

  • Not all classes within your project are autoloaded, the autoloaded files are specified by autoload.yml (in *sf_lib_dir/config/config*). You can create your own autoload.yml and drop it into the project config dir (or even application config dir I think).
  • When running in dev environment, if a class cannot be autoloaded then symfony will rebuild the autoload cache hoping to find the file its assuming is new. This can make your app slow in certain use cases so use sfAutoloadAgain::unregister().
markymark