views:

64

answers:

3

Hi,

I'm trying to find the best pragmatic approach to import functions on the fly... let me explain.

Say I have a directory called functions which has these files:

array_select.func.php
stat_mediam.func.php
stat_mean.func.php
.....

I would like to: load each individual file (which has a function defined inside) and use it just like an internal php function.. such as array_pop(), array_shift(), etc.

Once I stumbled on a tutorial (which I can't find again now) that compiled user defined functions as part of a PHP installation.. Although that's not a very good solution because on shared/reseller hosting you can't recompile the PHP installation.

I don't want to have conflicts with future versions of PHP / other extensions, i.e. if a function named X by me, is suddenly part of the internal php functions (even though it might not have the same functionality per se) I don't want PHP to throw a fatal error because of this and fail miserably.

So the best method that I can think of is to check if a function is defined, using function_exists(), if so throw a notice so that it's easy to track in the log files, otherwise define the function. However that will probably translate to having a lot of include/require statement in other files where I need such a function, which I don't really like. Or possibly, read the directory and loop over each *.func.php file and include_once. Though I find this a bit ugly.

The question is, have you ever stumbled upon some source code which handled such a case? How was it implemented? Did you ever do something similar? I need as much ideas as possible! :)

+2  A: 

One way you could pull something like this off is to put those functions into classes and then set up an __autoload function. If you are against wrapping the functions in classes than this solution probably won't apply to you. Personally I like it because it allows me to namespace my functions and share private methods between them.

First you set up your autoload function similar to this. You'll want to adjust the naming convention to fit your own style, and probably introduce some error handling, but this is just to get the basic idea across.

function __autoload($class_name){
     require_once(strtolower("library/$class_name.class.php"));
}

Then anywhere in your code regardless of scope you can do something like this.

arrayFunctions::doStuff($myArray);

PHP will automatically try to include "library/arrayFunctions.class.php" and look for a method called "doStuff" in the arrayFunctions class.

Greg W
that may be one way to implement it. though why should I have a class that's just going to have one static method? I know, I'm hard to please. Actually, I wonder if it's possible to pass the params to __construct() and get a return value directly from the __construct().. something like calling __toString(); when you try to echo an object.
ninuhadida
Re "though why should I have a class that's just going to have one static method?"You can (and should!) put more than one method in the classes. Its great from an organizational standpoint because you essentially categorize your functions this way.
Greg W
A: 

I have issues with this idea. Hitting the file system to include a single function is very expensive in the terms of it lowering your max possible requests per second.

It's generally much better to load/parse five functions in a single file (static class?) and only use two of them (one stat call) rather than load two files for two functions (two stat calls).

Which obviously becomes even worse when you need all five functions.

Peter Bailey
so in that case, even Greg W's solution suffers..
ninuhadida
Only if you write one function per class, which I would consider a pretty abusive use of classes.
Peter Bailey
Well, I don't see why you should have a single file with all the classes and then use autoload. why not just use include/require in the first place then?
ninuhadida
Several reasons. One, because include/require are contextual from the first script in the execution stack, which can lead to all sorts of voodoo to obtain a proper filepath. Also, `__autoload()` removed all the headaches involved with using `require` vs `require_once`. There are other reasons but those two are enough. These all mean you can effectively do lazy-loading without over-burdening your system by unecessarily greedy-loading.
Peter Bailey
A: 
  1. To automatically load stuff when need, put your functions in classes and use autoloading.
  2. For the name conflict, use namespaces (if you have PHP 5.3).
Bart van Heukelom