简体   繁体   中英

Is it possible to user a variable when defining a PHP function's name?

I've built a CMS for our company which has a huge number of functions, many of which are related to specific functionality which isn't used on most of the sites we use the CMS for. My goal, then, is to only include the script of a function when it's needed (when the function is called).

Right now I'm simply calling up each function as normal, requiring the file where the actual script of the function is located, and then calling a second function (the same name as the function, but with an underscore prefix) which contains the actual script of the function. For example:

function foo($a,$b) {
    require_once "funtions-foo.php";
    return _foo($a,$b);
}

This, however, seems a little repetitive to me. What I'm looking for is a way to either 'catch' a functions call and, if its name is in an array of 'included' functions, i'll include the correct file and call the function. For example:

catch_function_call($function,$arg1,$arg2) {
    $array = array(
        'foo' => 'functions-foo.php',
        'bar' => 'functions-bar.php'
    );
    if($array[$function]) {
        require_once $array[$function];
        return $function($arg1,$arg2);
    }
}

There I'm assuming the 'catch_function_call' function can somehow catch when a function is called. As I know of no such function, however, my second thought was to simply define each of the 'included' functions using variables. For example:

$array = array(
    'foo' => 'functions-foo.php',
    'bar' => 'functions-bar.php'
);
foreach($array as $function => $file) {
    function $function($arg1,$arg2) {
        $_function = "_".$function;
        require_once $file;
        return $_function($arg1,$arg2);

    }
}

Of course, this gives me an error as I apparently can't use a variable in the name of a function when defining it. Any thoughts on how to get around this or other solutions for only including a function when it's needed?

You can use __call or __callStatic on an object or class, respectively, which would approximate the functionality you're looking for. You can read some explanation in this thread .

However there's no way to do this in the global function space.

Could this help: http://php.net/manual/en/function.create-function.php ? Or maybe turn the design to OO and use __call() .

只需在每个需要的功能之前使用include_once

Have you considered grouping your functions into sets and storing them as static methods in objects. Then you can use the __autoload() function and stop worrying about when to include.

So:

class Rarely_Used{

  static function foo(){}
  static function bar(){}
}

class Only_for_Managers{
  static function ohboy(){}
  static function punish(){}
}

Why is including all the files such a big problem? As you probably are using APC heavy filesystem access won't be a problem. And apart from that, lookups in the function hash table obviously are slower if there are more functions, but still this most certainly will not be the bottleneck of your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM