Why does autoload not work

Hi

In /config/main.php I have




	// autoloading model and component classes

	'import'=>array(

		'application.models.*',

		'application.components.*',



In the components folder I have a file called slankeplanen.php which contains several classes.

In a controller I try this:




$weekplan = new week_plan_html($id);



this trigger the yiibase.php autoload() method, which it should not do as the class should already have been included by the configuration, or ???????

Kind regards

Steen

When you use a new class (not yet loaded)… Yii try to find that class in the "import" folders… but it looks for the same name…

So you need to have one file for one class… and the file name should be the same as the class name (case sensitive)…

Perhaps naming the main class the same as the file and including in the bottom the other classes will allow you to use just one file because when you call ‘new bla_bla()’ yii will include that file altogether with classes bla_bla1, bla_bla2 etc etc …

yes, it will… but what if in some other place you need to call the "other" class without calling the first class?

Yes you are absolutely right! I was assuming we are talking about a class that depends on other classes which are not going to be instantiated separately.

well, what is the point of the autoload if it looks for a class file first??

In my case the file contains 35 classes, that means that I need to split one file to 35 files… that will not improve performance :)

Most autoloaders work this way. Autoloader needs some hint of which file to load, and the easiest way to provide hint is to follow the filename-is-the-same-as-classname convention. You can put all of your classes in a single file and include that file at the beginning of your application, but parsing a lot of unnecessary code will degrade performance too.

Opcode caches (like APC) can help a lot when you run into problems with autoloading performance.

well if If I have to split my file into several small model file it will also degrade performance!

I know how the autoloading works, but I think that Yii have a minor issue here as I said, I added the file to my file to the ‘autoload’ section of the application configuration, which means that there is no need to autoload or include the file!

here is the ‘default’ part of /config/main-php




	// autoloading model and component classes

	'import'=>array(

		'application.models.*',

		'application.components.*',

	),



As you can see on the comment, it means that all files in these to folders should be loaded, and since I put my file in the components folder, I would expect it to be loaded, but it does not load and therefore the autoload() is triggered

Don’t forget that the main feature of Yii is that it’s fast… this is achieved by loading only components that are used…

NOTE: the "import" array does not mean import/load all that is in those directories… it just means search these directories when an "unknown" class is requested…

To be fast and effective no parsing of classes is done… instead just the filenames are checked…

Think about this like a PATH variable for the OS… when on the command line you enter a filename to be executed… the OS first checks the current directory to see if there is that file if it’s not it checkes all directories in the PATH to try to find the filename you want to execute… and it does that by checking filenames… not content of them…

In the end it all depends on your needs… if you really need all dose 35 classes at once… then its OK to put them in one file… you just have to include them manually when you need them… no problem for that… you can use the PHP include() or require()…

Well… loading 35 files instead of one is not faster!

Then the comment and documentation should be changed as it says that all the files will be loaded!

You are not even close to the truth.

it only load a class in you specify the 100% correct class name e.g if you have MyClass in a file called xx.php it will NOT work, so your PATH logic does not work.

I need the 35 classes, that’s why I put them in the autoload section of the configuration

I don’t see the point in this discussion any more…

I explained to you how the Yii autoloader works… and that’s it…

For your particular case… just use include() and problem solved…