0

I have an old php function in a .php files and want to include and call this from my controller. I copy my .php function file to myLib folder first and then defined my function in config file like this:

// autoloading model and component classes
    'import'=>array(
        'application.models.*',
        'application.components.*',
        'application.myLib.*',
    ),

now how to call my function in controller??

I call it simple by name but I get this error:

Fatal error: Call to undefined function myFunction() in C:\xampp\htdocs\test\protected\controllers\TestController.php on line 10

2
  • Where is the myLib folder located? Commented Nov 15, 2013 at 14:31
  • Inside protected folder. Commented Nov 15, 2013 at 14:34

1 Answer 1

1

The Yii autoloader is meant to load classes, as in, it will load the file for a class if the class is called. So this does not work for a singleton function in a file.

What you could do is make a helper class and add the function as a static method to the class.

for example :

class MyLib
{
    public static function myFunction()
    {
          //do stuff here
    }
}

and then you can just call out your function like you did before with

MyLib::myFunction();

Then store that class in the file /protected/helpers/MyLib.php

and in the config do:

'import'=>array(
    'application.models.*',
    'application.components.*',
    'application.helpers.*',
),
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.