0

I use this code to load classes

 function __autoload($className)
 {
      $files = dirname(__FILE__).'/public/class/'.$className.'.php';

      if(file_exists($files))
      {
           include_once($files);
      }
 }

does anyone know how to retrieve the function automatically, too? Thank you.

0

2 Answers 2

3

Autoloader function registered spl_autoload_register() can be used to load classes, but not functions. Wrap your functions in class or classes to utilize autoloading, i.e.

class Utils {
  static function foo() {
    ..
  }
}

then call it static way:

Utils::foo();

and you can have it autoloaded when needed. See more infromation on autoloading in PHP manual.

Sign up to request clarification or add additional context in comments.

2 Comments

Too do not understand what you mean, an example? Thank you very much
Just add static keyword to function declaration as in my updated answer.
0

Yes, you can automatically load classes using the autoload feature, but no, you can't do the same for functions.

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.