1

Possible Duplicate:
autoload functions in php

I am working on a PHP framework. I wonder if there is a way to maybe rewrite the error handler when a function doesn't exist to automatically try to include the file stating the function first.

Example:

echo general_foo(); // <-- general_foo() is not yet stated.
                    // A handler tries to include_once('functions/general.php') based on the first word of the function name.
                    // If the function still doesn't exist - throw an error.

The win from this would be to skip compiling unnecessary files or to skip keeping track and state includes here and there.

Simply __autoload for functions rather than classes.

Community
  • 1
  • 1
tim
  • 2,530
  • 3
  • 26
  • 45
  • If it's a personal/internal framework, you could jerry rig something like f('fn', array([args])); but I don't think the trade-off is worth it. – Paolo Bergantino Jul 05 '12 at 22:25
  • possible duplicate of [autoload functions in php](http://stackoverflow.com/questions/4196881/autoload-functions-in-php) - err - [Autoloader for functions (19 Jan 2011)](http://stackoverflow.com/questions/4737199/autoloader-for-functions) – hakre Aug 27 '12 at 10:26

1 Answers1

2

It does not exists and probably will never. Yes, I would like it too... However, this does not prevent you from using classes with static functions and let PHP autoload.

http://php.net/spl-autoload-register

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • So by calling a method rather than a function I would be able to accomplish something similar inside the class events? – tim Jul 05 '12 at 21:50
  • Yes. I've added a link to the answer. But you should google for "PSR-0" and "PSR-0 Classloader" first (and because you are already googling: "Composer" ;)), so you don't need to reinvent the wheel. – KingCrunch Jul 05 '12 at 21:53
  • Solved by using __call(), see my posted solution. – tim Jul 05 '12 at 23:56