Startup functions in php?

I have this problem that I have a lot of huge functions and I use only a few in the given script. Each function is in its own file. It would be nice to be able to "autoload", or rather, require_once a file if this function does not exist.

Maybe there is a way to override Fatal error: Call to undefined function... at the beginning of the script, so every time this error is launched in the script, you first need to try to require a file name with the name of a nonexistent function, and then try calling the function again.

0
php
Jul 25 '11 at 7:32
source share
7 answers

Since php 5.3.0 , you can do the following:

 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(); 

That will try to upload the file to funcs/helloworld.func.php and execute helloworld after a successful download.

This way you can omit the repeated inline tests.

+3
Jul 25 2018-11-11T00:
source share

function_exists

and the code might look like this:

 if ( !function_exists('SOME_FUNCTION')) { include(.....) } 
+2
Jul 25 '11 at 7:35
source share

If you use scripts 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 cuntion

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

if you use classes, for example. OOP. than you can use the __ autoload method:

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

// Now you can use classes that were NOT included in the current file.

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

+1
Jul 25 2018-11-11T00:
source share

function_exists will be better

0
Jul 25 2018-11-11T00:
source share

I think you could try writing some error handling that includes function_exists functions, but the problem is when to load the function correctly?

Have you considered the possibility of integrating your functions into classes so that you can use http://uk.php.net/autoload ?

0
Jul 25 2018-11-11T00:
source share

Undefined Function errors are fatal errors for PHP. Thus, there is no way to deal with fatal errors (except for hacks like register_shutdown_function ). It is better to think about OOP method and use classes with __ autoload .

0
Jul 25 2018-11-11T00:
source share

function_exists will not help you catch non-existent functions on the fly. Instead, you have to surround all calls to the if (! Function_exists ()) functions. As far as I know, you can capture non-existing calls on the fly using classes using _ autoload . Maybe it would be advisable to put your code in classes or put a related set of functions in a single file and, thus, save a series of checks for the functions needed for the function_exists?

0
Jul 25 2018-11-11T00:
source share



All Articles