PHP函数传递包含文件

时间:2012-12-04 02:42:39

标签: php html css web

我正在尝试制作一个loadPlugin(插件)函数,该函数将从插件文件夹中获取并获取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);
}

4 个答案:

答案 0 :(得分:1)

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

if( file_exists("scripts/plugins/".$name)) include("scripts/plugins/".$name));
elseif( strtolower($debug) == "d") trigger_error("....");

然后,加载一个插件:

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

您可以根据需要多次添加单个文件,只要他们不尝试重新定义功能即可。所以这基本上就像在全局范围内调用的函数一样。

答案 1 :(得分:1)

加载一个具有该功能的文件似乎很不对劲。为什么不创建类并采用面向对象的方法?

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

就“全局”而言,我可以想到一些关于持久性的不同路径,这些路径围绕使用会话变量来存储每次加载新页面时填充对象的信息。甚至可以将对象本身存储到会话中(这不是最佳实践,但仍然可行)。

答案 2 :(得分:1)

好吧,我遇到了类似的问题,并决定寻求可重用和可扩展的东西。 所以,我只是为了演示科林的答案而修剪它。代码可以通过改进:)

<?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)
        );
   }


 }

  ?>

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

答案 3 :(得分:0)

为示例$ pluginVars创建全局数组,并在内部插件文件中存储您想在此数组外部使用的所有变量。