Php 将函数从外部文件动态加载到类中

Php 将函数从外部文件动态加载到类中,php,class,Php,Class,在php中是否可以加载一个函数,比如从外部文件加载到类中。 我正在尝试为助手函数创建加载程序,以便调用: $registry->helper->load('external_helper_function_file'); 在此之后,它应该能够调用文件中的函数,如下所示: $registry->helper->function(); $registry->aHelper->aFunction(); $registry->aDifferentHelper

在php中是否可以加载一个函数,比如从外部文件加载到类中。 我正在尝试为助手函数创建加载程序,以便调用:

$registry->helper->load('external_helper_function_file');
在此之后,它应该能够调用文件中的函数,如下所示:

$registry->helper->function();
$registry->aHelper->aFunction();
$registry->aDifferentHelper->aDifferentFunction();

感谢您的帮助

所以您不仅希望
包含
一个文件,而且希望
将其包含到对象的作用域中

我认为你走错了方向。如果
注册表
对象有一系列助手成员,这些成员有自己的函数,则更有意义。最终结果可能如下所示:

$registry->helper->function();
$registry->aHelper->aFunction();
$registry->aDifferentHelper->aDifferentFunction();
通过仔细使用{}语法,您应该能够动态地将成员对象添加到god对象中

在这一点上,值得注意的是a几乎是不变的。如果您在全局范围内需要这些函数,请使用引导包含技术,然后将其放入全局范围。如果需要传递这些数据,则可以根据需要将其传递给函数,或者将其存储在数据库中并在其他位置检索


我知道上帝客体看起来确实是个好主意,但我向你保证,它会让事情变得一团糟。

好的,首先,我同意这是不礼貌的。此外,在5.3中,您可以使用新的闭包语法和_call magic word将运算符用作函数(JS样式)

现在,如果我们想以你的方式提供一种实现方法,我可以考虑使用create\u fnuction,混合称为magic

基本上,您可以使用正则表达式模式将函数转换为兼容字符串,并将其放入私有成员中。然后使用_调用方法获取它们。我正在做一个小的演示

好的,上课时间到了。我从几周前看到的一个类中得到了灵感,该类使用闭包实现JS样式的对象:

/**
 * supplies an interface with which you can load external functions into an existing object
 * 
 * the functions supplied to this class will recive the classes referance as a first argument, and as 
 * a second argument they will recive an array of supplied arguments.
 * 
 * @author arieh glazer <arieh.glazer@gmail.com>
 * @license MIT like 
 */
class Function_Loader{
    /**
     * @param array holder of genarated functions
     * @access protected
     */
    protected $_funcs = array();

    /**
     * loads functions for an external file into the object
     * 
     * a note- the file must not contain php tags.
     * 
     * @param string $source a file's loaction
     * 
     * @access public
     */
    public function load($source){
        $ptrn = '/function[\s]+([a-zA-Z0-9_-]*)[\s]*\((.*)\)[\s]*{([\w\s\D]+)}[\s]*/iU';
        $source = file_get_contents($source);
        preg_match_all($ptrn,$source,$matches);
        $names = $matches[1];
        $vars = $matches[2];
        $funcs = $matches[3];
        for ($i=0,$l=count($names);$i<$l;$i++){
            $this->_funcs[$names[$i]] = create_function($vars[$i],$funcs[$i]);
        }
    }

    public function __call($name,$args){
        if (isset($this->_funcs[$name])) $this->_funcs[$name]($this,$args);
        else throw new Exception("No Such Method $name");
    }
}

这对我来说是一个有趣的练习,但总的来说是一个糟糕的实践

抛开观点不谈,这是一个好的OOP设计。即使使用当前版本的PHP,这也是可能的,尽管不像PHP5.3那样干净

class Helper {
    /* ... */
    function load($file) {
      include_once($file);
    }
    function __call($functionName, $args) {
       if(function_exists($functionName))  
         return call_user_func_array($functionName, $args);
    }

}

$registry只是一个用于存储函数和变量以供全局访问的类。感谢您的输入。这些辅助函数可能会不断增加。所以我只想在需要的时候加载它们。如果有人能指出正确的方向那就太好了。比如CI助手我建议你打开CodeIgniter代码库,好吗?如果这是您的理想模型,那么这听起来是一个很好的起点。您可能希望使用call_user_func_array(),在您的示例中,函数将在一个数组参数中传递所有参数,这正是我所寻找的。include文件中运行的内容的范围不保留在类中。这些函数可以在它们之外运行。