代码之家  ›  专栏  ›  技术社区  ›  Palantir

php宏/内联函数(避免变量超出范围)

php
  •  0
  • Palantir  · 技术社区  · 15 年前

    我有以下困境。我有一个复杂的CMS,这个CMS是由一个平面设计师的主题。模板是纯html,包含多个嵌套的包含。我想通过查看页面的html,使设计者更容易找到要修改的文件。

    我最初的想法是建造这样愚蠢的东西:

    function customInclude($what) { 
      print("<!-- Including $what -->");
      include($what);
      print("<!-- End of $what -->");
    }
    

    但是,你猜怎么着?变量显然超出了包含文件的范围:-)我不能将它们声明为全局变量或参数,因为我不知道如何调用它们以及它们有多少。

    有没有可能在php中实现某种“宏扩展”?另一种调用方法:我想以面向方面的方式修改modify函数的每个调用。

    我考虑过eval(),这是唯一的方法吗?它会对性能有很大影响吗?

    4 回复  |  直到 15 年前
        1
  •  1
  •   Fabrizio    14 年前

    我知道这是一个老问题,但我偶然发现了它,它让我想起了我曾经做过的事情。

    如果你用一个非常奇怪的变量来创建函数呢?

    <?php
    function customInclude($___what___) { 
      echo '<!-- Including '.$___what___.' -->';
      include($what);
      echo '<!-- End of '.$___what___.' -->';
    }
    ?>
    

    我通常建议添加一个可能的变量,只在必要时显示这些标记,你不想让其他人知道…

    <?php
    function __printIncludeInfo($info, $dump = false){
       //print only if the URL contains the parameter ?pii
       //You can modify it to print only if coming from a certain IP
       if(isset($_GET['pii'])){
           if($dump){
              var_dump($info);
           } else {
              echo $info;
           }
       }
    }
    function customInclude($___what___) { 
      __printIncludeInfo('<!-- Including '.$___what___.' -->');
      include($what);
      __printIncludeInfo('<!-- End of '.$___what___.' -->');
    }
    ?>
    

    这样,您就可以使用该函数打印所需的任何其他信息

        2
  •  1
  •   NullUserException Mark Roddy    14 年前

    我不确定我是否完全理解这个问题,但如果您只是想通过向设计器显示包含文件的底层文件名来简化其工作,那么您可能只需在模板文件中使用它:

    echo '<!-- Start of '.__FILE__.' -->';
    ....content... 
    echo '<!-- End of '.__FILE__.' -->';
    

    __FILE__ 只是其中之一 Magic Constants

    还有 get_included_files() 函数,该函数返回所有包含文件的数组,这些文件可能会被使用(例如,您可以输出名称为“tpl”的所有包含文件的列表)。

        3
  •  1
  •   glerendegui    14 年前

    这是我对自定义包含问题的100%共享编码解决方案。它使用一个全局变量指向下一个包含文件名,然后包含我的自定义代理包含文件(替换自定义代理包含函数)

    1-将此代码添加到全局include(定义custominclude函数的位置)

        $GLOBALS['next_include'] = "";
        $GLOBALS['next_include_is_once'] = false;
        function next_include($include_file) {
            $GLOBALS['next_include_is_once'] = false;
            $GLOBALS['next_include'] = $include_file;
        }
    
        function next_include_once($include_file) {
            $GLOBALS['next_include_is_once'] = true;
            $GLOBALS['next_include'] = $include_file;
        }
    

    2-创建一些include代理包含文件,例如“debug\u include.php”

        <?php
        if(empty($GLOBALS['next_include'])) die("Includes Problem");
    
        // Pre-include code
        // ....
    
        if($GLOBALS['next_include_is_once']) {
            include_once($GLOBALS['next_include']); 
        } else {
            include($GLOBALS['next_include']);
        }
    
        // Post-include code
        // ....
    
        $GLOBALS['next_include'] = "";
    

    3-在所有文件中执行搜索和替换:(debug_include.php除外)
    search:'包含(.*);'作为reg.exp
    替换为:'{next_include($1);include('debug_include.php');}'

    search:'包含一次(.*);作为reg.exp
    替换为:'{next_include_once($1);include('debug_include.php');}'

    如果你有一些非标准的包含,比如

        include (....     include  (....       include   (.... 
    


    我认为您可以找到一些更好的搜索和替换模式,但我不是正则表达式用户,所以我很难做到这一点。

        4
  •  -6
  •   Mikulas Dite    15 年前

    您一定要使用对象、名称空间和mvc模型。否则就有 没有纯净的溶液 对你的问题。求你了,不要用eval,它是邪恶的。

    推荐文章