0

I have this problem that I have many huge functions, and I'm using only few in a given script. Every function sits in its own file. It would be nice to be able to 'autoload' or rather require_once a file when given function doesn't exists.

Maybe is there a way to override Fatal error: Call to undefined function... at the beginning of a script, so everytime that error fires up the script would first try to require_once a file name with name of a non existent function, and then try to call the function again.

rsk82
  • 28,217
  • 50
  • 150
  • 240
  • Related: [Autoloader for functions (19 Jan 2011)](http://stackoverflow.com/questions/4737199/autoloader-for-functions) – hakre Aug 27 '12 at 10:25

7 Answers7

4

Since php 5.3.0 you could do someting like:

class Funcs
{
    public function __callStatic($name, $args) {
        if (!function_exists($name)) {
            require_once sprintf(
                'funcs/%s.func.php', // generate the correct path here
                $name
            );
        }

        if (function_exists($name)) {
            return call_user_func_array($name, $args);
        }
        else {
            // throw some error
        }
    }
}

And then use it like (for example):

Funcs::helloworld();

Which would try to load a file in funcs/helloworld.func.php and execute helloworld after successfull loading.

This way you could ommit the repeated inline tests.

Yoshi
  • 54,081
  • 14
  • 89
  • 103
  • A great answer, just like the one I came up with for PHP <5.3 at http://stackoverflow.com/questions/11352996/automatically-include-missing-functions :) Wish I saw this earlier. – tim Jul 06 '12 at 00:01
2

function_exists

and the code may be like this

if ( !function_exists('SOME_FUNCTION')) {
     include(.....)
 } 
Subdigger
  • 2,166
  • 3
  • 20
  • 42
1

If you are scripting without OOP, you can use the function exist function:

if(!function_exists('YOUR_FUNCTION_NAME')){
    //include the file
    require_once('function.header.file.php');
}

//Now call the cuntion

//Reference: http://php.net/manual/en/function.function-exists.php

if you are using classes eg. OOP. than you can use the __autoload method:

function __autoload($YOUR_CUSTOM_CLASS){
    include $YOUR_CUSTOM_CLASS.'class.php';
}

//Now you can use classes you have NOT included in your current file.

//Reference: http://php.net/manual/en/language.oop5.autoload.php

Ext
  • 657
  • 1
  • 8
  • 13
0

function_exists will do better

k102
  • 7,861
  • 7
  • 49
  • 69
0

I guess you could try to write some error handling which includes function_exists, but the problem is determining when to load the function right?

Have you considered integrating your functions into classes so you can leverage http://uk.php.net/autoload?

akhumphrey
  • 31
  • 5
0

Undefined function errors are fatal errors for PHP. So there's no way to handle fatal errors (except hacks like register_shutdown_function). It's better to think OOP way and use classes with __autoload.

useraged
  • 1,706
  • 17
  • 34
0

function_exists will not help you to catch the non-existing functions on the fly. Instead, you will have to surround all the function calls with if (!function_exists()). As far as I know, you can only catch non-existing calls on the fly with classes, using _autoload implementations. Maybe it would make sense to put your code into classes or put the related set of functions into one file and thus save a number of checks for function_exists required?

Aurimas
  • 2,518
  • 18
  • 23