"autoloading" question


I’m not sure, how the autoloading of extension works at the following situation:

I have 2 extension, which are not needed all the time, "extensionA" and "extensionB".

And: there are also exists on component called myGlobalFunction.php, where different method resist, which i need in some cases.

At components/myGlobalFunction.php there are functions/methods which uses only extensionsA (function useExtA() ) but there are also functions/methods which only uses extensionB (useExtB() ).

Imagine the following situation: one instance of the application uses the function useExtA() from components/myGlobalFunction.php which needs the extensionA.

The question is: is extensionB loaded at this time (because components/myGlobalFunction.php is loaded which require extensionA and extensionB even the called function useExtA() does not require extensionB ?

…I hope this question is understandable. :-/

In this case it would be better to create 2 components, like components/myGlobalFunctionForExtensionA.php (which only includes function useExtA()), right?!

Thank you.

as I understand - you have got two conflicting libraries (in some function/class naming) but they are never used together, right?

you can influent autoloading by manually specyfying Yii::import() just before component must be used:

function useExtA() {

  Yii::import( 'ext.extensionA.*' );

  //work with extension A library


function useExtB() {

  Yii::import( 'ext.extensionB.*' );

  //work with extension B library


if there are no chances to run both functions (for example unittests with 100% code coverage :) ) it should work. the extensions directories shoud not be placed in default search scope (‘import’ part of config file) as it can influence search order and lead to some errors (you should not assume any particular order and precendce of Yii::import() over default ‘imports’).

Another note - one of Yii’s principles is to load any needed componenets (and it means ‘include files’ and ‘instantiate objects’) only when it is needed. So if you place Yii::import() in code, not in php file header that is always parsed and run - they will not be referenced at all.

Thank you, redguy.

No, there are not conflicting. It’s not a problem if they are loaded at the same time. This question only points to save “parsing time” and disk I/O to load only thinks if the are needed.

>Another note - one of Yii’s principles is to load any needed componenets

That’s the point (and the question). When is it needed?

For example the applications only used useExtA() from components/myGlobalFunction.php

So Yii have to load and to parse the file components/myGlobalFunction.php … including the following text:

function useExtA() {

  //processing things which are exists in extension A library


function useExtB() {

  //processing things which are exists in extension B library


Even if useExtB() is not used in the application, the file components/myGlobalFunction.php provides this function. So i could imagine, that for parsing the file components/myGlobalFunction.php Yii have to load all the realted (included) files, to parse/understand the content of myGlobalFunction.php.

So in this situation I’m not sure if the autoloader loads “extensionB” or does not (because useExtB() is not uses)

That means: if the autoloader loads all needed extension to “understand”/parse the content of myGlobalFunction.php it’s better to split this component depending on the related extensions, like:

components/myGlobalFunctionForExtentionA.php and components/myGlobalFunctionForExtentionB.php

'autoloader" is doing this. it “auto loads” files when they are needed. for example if you use class that does not have been declared yet - autoloader will try to load its definition and by the nature of PHP itself it does not need definition of class just because it is used somwhere in file. PHP needs the definition only when running code references such class.

class X {

  function useExtA() {

    $tmp = new componentA();



in this example componentA is not referenced neither when this code is loaded or class X instantiated. It is done only when useExtA function is called and then autoloader will be called to find "componentA" definition.

You can check that behavior simply - by putting

echo "Here we are in libA";

in your library file and check if the text is shown or not.

ah… okay, thank you for that! :slight_smile: