duancha1065 2014-05-03 02:05
浏览 25

多个同名的功能

I am looking to create an extension api for my web application.

Example extension file:

function echoCustomHeaders(){
    echo '<li><a href="./watever">Header Link</a></li>';
}

There would be several files similar to the example extension file (with the same function name, for user friendlyness when programming addons).

for($x=0;$x<count($extension_files);$x++){
    //This would obviosely break when it gets to the second file, as functions cannot be declared twice in php
    require_once($extension_files[$x]);
}
//some code later...

//this should call echoCustomHeaders() in ALL of the extension files, what code should I put here to make this happen?
echoCustomHeaders();

In case you are wondering about what the question is, read the comments in the code above and it should be fairly easy to see.

  • 写回答

2条回答 默认 最新

  • doubairan4213 2014-05-03 02:21
    关注

    Return closures (lambda expressions) in your extension files as follows:

    return function(){
        echo '<li><a href="./whatever">Header Link</a></li>';
    }
    

    In PHP the include/require statement is really a function and therefore has a return value, hence you can collect those closures into an array:

    $closures = array();
    for($x=0;$x<count($extension_files);$x++){
        $closures[$i]=include($extension_files[$x]);
    }
    
    // Do whatever you want with your closures, e.g. execute them:
    foreach($closures as $closure) {
        $closure();
    }
    

    ADDED CONTENT:

    In the case if you would like to return multiple closures with each include, you may return an array of closures, indexed by the name of them:

    return array(
      'echoCustomHeaders' => function() {
        echo '<li><a href="./whatever">Header Link</a></li>';
      },
      // ...
    );
    

    Then you can still execute some of them by their name:

    $closureArray = array();
    foreach($extension_files as $file) {
        $closureArray[] = include($file);
    }
    
    foreach($closureArray as $closure) {
        if(isset($closure['echoCustomHeaders'])) // Maybe there wasn't an echoCustomHeaders in each extension file ...
            $closure['echoCustomHeaders']();
    }
    

    Maybe it would be a better idea to even separate the different kind of extension functions into distinct arrays:

    $closureArray = array();
    foreach($extension_files as $file) {
        $functions = include($file);
        foreach($functions as $name => $function) {
            if(!isset($closureArray[$name]))
                $closureArray[$name] = array();
            $closureArray[$name][] = $function;
        }
    }
    
    foreach($closureArray['echoCustomHeaders'] as $closure) {
            $closure();
    }
    

    Another solution is to use a more object oriented way, and declare a new extension class in each extension file. However, if there would be no data sharing required between the extension methods in an extension file, then simply returning the functions as an array of closures is a more lightweight and cleaner solution in my opinion.

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比