传递包含文件的PHP函数

传递包含文件的PHP函数,php,html,css,web,Php,Html,Css,Web,我试图制作一个loadPlugin(plugin)函数,从plugin文件夹中获取php文件,并将其变量/函数加载到站点中 问题是我们都知道函数不是全局的,这就引出了我的问题 由于任何文件都可能包含在内,我不能将文件中的每个变量都设置为全局变量,因为它们可能是随机的和无限的,我希望将其设置为文件中的任何函数都可以在页面上的任何位置使用 任何关于如何做到这一点的建议都是非常好的。谢谢 这就是我到目前为止所做的: function loadPlugin($name,$debug='') {

我试图制作一个loadPlugin(plugin)函数,从plugin文件夹中获取php文件,并将其变量/函数加载到站点中

问题是我们都知道函数不是全局的,这就引出了我的问题

由于任何文件都可能包含在内,我不能将文件中的每个变量都设置为全局变量,因为它们可能是随机的和无限的,我希望将其设置为文件中的任何函数都可以在页面上的任何位置使用

任何关于如何做到这一点的建议都是非常好的。谢谢

这就是我到目前为止所做的:

function loadPlugin($name,$debug='') {
    if(file_exists("scripts/plugins/".$name)) {
        include("scripts/plugins/".$name);
    } else if(strtolower($debug)=='d') trigger_error($name." does not exists in the plugins folder.", E_USER_ERROR);
}

您可以尝试创建一个名为
loadPlugin.php
的文件,而不是调用该函数,代码如下:

if( file_exists("scripts/plugins/".$name)) include("scripts/plugins/".$name));
elseif( strtolower($debug) == "d") trigger_error("....");
然后,要加载插件:

$name = "pluginName"; include("loadPlugin.php");

您可以随意多次包含单个文件,只要它们不尝试重新定义函数。因此,这基本上与在全局范围内调用的函数类似。

创建全局数组,例如$pluginVars和inside plugin file,将您希望在该数组外部使用的所有变量存储在该数组中。

加载一个具有其功能的文件似乎不可靠。为什么不创建类并采用面向对象的方法呢

您可以加载可以在整个代码中重用的对象。您甚至可以实现一个接口,该接口将保证在所有插件中都存在某些方法和属性,而不管是谁开发它们


就“全局”而言,我可以想到,在持久化方面,您可以走几条不同的道路,即使用会话变量存储信息,以便在每次加载新页面时填充对象。甚至可以将对象本身存储到会话中(这不是最佳实践,但仍然是可能的)

嗯,我也遇到了类似的问题,决定开发可重用和可扩展的东西。 所以,为了演示科林的答案,我把它整理了一下。但代码可以改进:)


它使用PHP的自动加载函数,因此请确保您拥有这些函数。 为了解决冲突,我添加了一个小的注册表类(用于唯一变量和函数的键/值存储)来修复冲突。 它还提高了性能。
对你这样的工作来说,这可能是一种过火的行为,但这对我很有帮助,因为我的工作是一个相当大的项目。

我现在基本上就是这么做的。我只是想让打电话给他们更容易,对那些同样编辑我的代码的人来说更友好。我可以命名一个数组,然后循环遍历它,每次只调用loadPlugin()一次,就像你在这里说的那样。假设它不是一个变量,而是一个函数,你能使该函数全局化吗?我正在尝试更多地使用函数,因为我以前从未使用过函数,因为它们会让我很恼火。是否最好总是加载我所有的函数,而不是与页面相关的函数?未使用的函数是否会降低页面加载速度等?我的意思是,我不认为下载一个包含10-15个更大函数的文件有那么糟糕,但我认为最好只保留每页所需的内容。PHP代码不会发送到客户端。它在服务器上呈现。
<?php
/**
* Autoloader
* @Usage: 
*       // set autoload paths
*       Loader::setPaths(
*               array("interface_path"=>dirname(__FILE__). "/core"),
*               array("plugin_path"=>"/var/www/awesome-app/plugins"),
*               array("controller_path"=>dirname(__FILE__). "/controllers")
* );
* 
* 
*   // Now, the magic
*       Loader::registerAutoloads();
*/

class Loader{

protected static $interfacePath = '';
protected static $pluginPath = '';
protected static $controllerPath = '';

/**
 * Set paths to autoload classes
 *
 * @param string $application
 * @param string $library
 * @todo this part can be cleaner/smarter with less code.
 *  Replace "" for with constants as default folders to search
 */
public static function setPaths($autoload_paths= null)
{
    if(is_array($autoload_paths)){
        self::$interfacePath = array_key_exists("interface_path", $autload_paths) ? 
                        $autoload_paths["interface_path"] : "";

        self::$interfacePath = array_key_exists("plugin_path", $autload_paths) ? 
                        $autoload_paths["plugin_path"] : "";

        self::$interfacePath = array_key_exists("controller_path", $autload_paths) ? 
                        $autoload_paths["controller_path"] : "";

    }       
}


/**
 * Registers autoload functions
 *
 */
public static function registerAutoloads() {
    spl_autoload_register('Loader::loadInterface');
    spl_autoload_register('Loader::loadPlugin');
    spl_autoload_register('Loader::loadController');
    if ( function_exists('__autoload') ) {
        spl_autoload_register('__autoload');
    }
}

/**
 * Checks if a given file exists and load it
 *
 * @param string $filename
 * @return bool
 */
protected static function check($filename)
{
    if(file_exists($filename)){
         include_once $filename;
        return true;
    }
    return false;        
}


/**
 * Interface Loader
 *
 * @param string $className
 * @return bool
 */
static function loadInterface($className)
{
    return self::check(
        sprintf('%s/%s_interface.php',self::$interfacePath, low($className))
    );
}


/**
 * Plugin Loader
 *
 * @param string $className
 * @return bool
 */
static function loadPlugin($className)
{
    return self::check(
        sprintf('%s/%s_plugin.php',self::$pluginPath,low($className))
    );
}



/**
 * Controller Loader.
 *
 * @param string $className
 * @return bool
 */
static function loadController($className){
    $fileName = camelCaseToUnderscore($className);
    return self::check(
        sprintf('%s/%s_controller.php',self::$controllerPath,$fileName)
        );
   }


 }

  ?>