4

Is it possible to autoload functions?

What I have is I have written functions distributed over different files named after the function name, so what I need is to autoload the file containing the function automatically. Is there any way to do this?

Charles
  • 50,943
  • 13
  • 104
  • 142
sreejith
  • 45
  • 2
  • 6
  • See my solution at http://stackoverflow.com/questions/11352996/automatically-include-missing-functions – tim Jul 05 '12 at 23:57
  • Related: [Autoloader for functions (19 Jan 2011)](http://stackoverflow.com/questions/4737199/autoloader-for-functions) – hakre Aug 27 '12 at 10:24

5 Answers5

5

You can autoload classes, so if you make your functions static methods of classes then it will work.

abstract class Util
{
    static function doSomething() {

    }
}

Usage:

Util::doSomething();
hakre
  • 193,403
  • 52
  • 435
  • 836
Piotr
  • 4,813
  • 7
  • 35
  • 46
  • 1
    Hi Piotr, Yes, I know we can autoload the class, and what I'm looking for is some way to autoload the functions without using it inside a class. It will be great if you can tell me how to do it. – sreejith Nov 16 '10 at 17:16
  • Sorry I do not know if that is possible considering this would break any code optimization and bytecode caching. – Piotr Nov 16 '10 at 17:22
  • sreejith, there is no way to do this without using Piotr Blasiak's recommendation, which is in fact a good recommendation. It will make your code a bit more usable as well. Right now, I am in the process of doing this for a large application. The result is going to be very well organized functions. (Each function will be categorized inside of a class, meaning all related functions will be a member of the same class. Making it more usable and easy to understand. It will also allow less includes when function calls are needed. This answer should be marked as the correct answer. – teynon Nov 10 '12 at 19:08
1

Use:

include("path");

or

require_once("path");

References:

http://php.net/manual/en/function.include.php

http://php.net/manual/en/function.require-once.php

Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
  • 1
    Hi Evan, I need to autoload the function dynamically like using the autoload() for classes. – sreejith Nov 16 '10 at 17:15
  • @ Webnet, unfortunately I can't do that, I need the code placed inside a function and to include that particular file having same name as of the function automatically ( IF POSSIBLE) – sreejith Nov 16 '10 at 17:19
1

Nope, but you can load classes. using __autoload($className)

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ivan
  • 3,567
  • 17
  • 25
0

Not directly. But you can add following code to top of your code to automatically include functions:

call_user_func(function($p,$w){$c=file_get_contents(__FILE__);$fs=explode('(',$c);$f=[];for($i=65;$i<=90;$i++){$vc[chr($i)]=1;$vc[chr($i+32)]=1;if($i<75)$vc[chr($i-17)]=1;}$vc['_']=1;foreach($fs as $fn){$fn=rtrim($fn);for($i=strlen($fn)-1;$i>=0;$i--){if(!isset($vc[$fn[$i]])){$f[]=substr($fn,$i+1);break;}}}foreach($f as $c){@include_once($p.$w[0].$c.$w[1]);}},
"func_dir/",["func_",".php"]);

The only thing you need to change is the second line: First parameter is the folder where to look for files, the second param is an array which wraps both values around the function name.

For example: If your function files are in the sub directory "func_dir/" and are namen "func_*.php" (where * is the function name), then you can use the above code directly as-is. However, you have to put that code in every file where you want to load functions automatically and adapt the path.

It's a little bit dirty, but it works. I hope my code helps you.

StanE
  • 2,704
  • 29
  • 38
  • Sometimes, regexes are the answer (or, at least, are simpler than the non-regex implementation). `call_user_func(function($prefix,$suffix){preg_match_all('/(\w+)\(/', file_get_contents(__FILE__), $matches);foreach($matches[1] as $func){@include_once("{$prefix}{$func}{$suffix}");}}, 'func_dir/func_', '.php');` is equivalent, but just as inefficient. Also, there's no reason to minify sample code or PHP code. – outis Dec 18 '15 at 02:26
0

Although this is not wise, it is possible.

You may save those functions in a file and prepend this file to all requested scripts:

  • front controller pattern (one central file for all requests, include it there)
  • php's auto_prepend_file

But the wise, OOP solution would be to group those functions into classes and use __autoload or some framework autoloader like Zend_Autloader to speed things up and load just the features you need.

takeshin
  • 49,108
  • 32
  • 120
  • 164