Static method of a module can't be seen

Hi all,

I have a module,‘user’,and is included in my main.config:




'import' => array(

        'application.models.*',

        'application.components.*',

        'application.modules.user.models.*',

        'application.modules.admin.models.*',

    ),

'modules' => array(        

        'user',

    ),

But when I try to call a static method of ‘user’ outside the module (say in /protected/views/site/index.php), which is ‘getAdmins’:


var_export(UserModule::getAdmins());



it is resulting to an include error:


include(UserModule.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory 

If I use the said line of code in other views under the ‘user’ module, it is working just fine.

Any ideas why? How can I make use of a module’s static function outside the module?

Hi mjkulet,

Try to include this line in your imports:




'import' => array(  

    ...      

        'application.modules.user.*',

    ...

    ),



From what I can see, you only included the user module’s models, and yet UserModule.php is outside the models folder. So basically what I suggested to you is to include what’s under the user module folder, which is UserModule.php.

I haven’t tested this out yet but I think this should work out just fine.

IIRC the UserModule class is in the directory application.modules.user

Try adding that one to the import array.

[edit]macinville beat me to it[/edit]

@ScallioXTX

:D

Hi! I have the same problem (calling a static function of this module).

When i try:


Yii::app()->getModule('user')->t("Register")

i get the error message:

Fatal error: Call to a member function t() on a non-object in …

This is my import array:


'import'=>array(

	'application.models.*',

	'application.components.*',

	'application.modules.user.*',

	'application.modules.user.models.*',

        'application.modules.user.components.*',

),

Seems that something goes wrong with the import/loading of the module.

Can anyone help? Any idea?

:(

I can see that you’re importing properly, but did you load the module?


'import'=>array(

	'application.models.*',

	'application.components.*',

	'application.modules.user.*',

	'application.modules.user.models.*',

        'application.modules.user.components.*',

),

'modules'=>array(

        'user',

),

Backing up though, I don’t understand why you’re putting a static function into a module like that. Static functions are typically called like so:


//<class name>::<public static function>

Yii::app()

I found the problem! Seems i had messed up the files of my application a little bit. ::)

I am not sure if this was what you meant by "loading the module"!

It was part of the code found in the “Installation Guide” of the module. So, i am not really sure about the reason he calls the method like that. But i had already tried to change this call into the form (class::method) that you mention and it didn’t work.

Whatever, thanks for your time!

I need the public static encrypting function in one of my controllers outside the module.

I do it so:




Yii::import('application.modules.user.UserModule)


$password=UserModule::encrypting($password);