【问题标题】:Is there a way to create a "custom" loop or control structure in PHP?有没有办法在 PHP 中创建“自定义”循环或控制结构?
【发布时间】:2015-11-10 23:49:42
【问题描述】:

我正在创建一个非常大的页面,该页面的各个部分都需要像这样循环遍历数据数组:

$processed = array();
foreach( $data as $program )
{
    if( $program['f_enabled'] == 'N' ) continue;
    $pid = $program['f_programId'];
    if( !array_key_exists($pid, $processed) )
    {
        /**
         *  Do things with $pid and $program['f_foobar'], including
         *  ?>
         *      <span>HTML with <?=$inlined_vars?></span>
         *  <?php
         */
        $processed[] = $pid;
    }
}

这让我想起了 WordPress Loop。我知道我可以将所有循环压缩为一个,将 HTML 输出存储在变量中并在最后将它们拼凑在一起,但我强烈希望代码与围绕它的 HTML 保持一致。

我怎样才能排除外面的一切?即使它像 hacky 一样:

MY_HORRIFYING_MACRO
{
        /**
         *  Do things with $pid and $program['f_foobar'], including
         *  ?>
         *      <span>HTML with <?=$inlined_vars?></span>
         *  <?php
         */
}

我不关心这里的正确性——我只需要这个演示工作,并且代码在演示文稿中从上到下可读,并同步了解页面上的其他内容。最好与 PHP 5.3 兼容——不肯定演示服务器将运行 PHP 5.4+——但如果存在使用 PHP 5.4+ 结构的解决方案,请无论如何分享。谢谢。

【问题讨论】:

  • 听起来你会从使用像Twig这样的现代模板语言中受益匪浅。
  • 树枝看起来棒极了。让我想起了 Angular,但似乎更适合模板化。不幸的是我没有时间/演示平台有一些不确定性......
  • 您应该花费 15 分钟来启动和运行 Twig。它的语法非常简单,文档非常好。你会花更多的时间来破解 PHP+HTML 这个怪物。
  • 有没有办法实现这一点,即使使用一些复杂的方法,比如带有heredoc函数体的函数? (我猜服务器会引发安全错误,但是......)
  • 你不需要 shell 访问 - 这是一个简单的 PHP 包 :-)

标签: php loops control-structure


【解决方案1】:

除了创建一个新的控制结构(就像在 Ruby 中所做的那样),您可以使用更多样板但更惯用的 PHP 来做一些事情,如下所示:

$processed = array();
function preloop($program) {
    if( $program['f_enabled'] == 'N' ) return true;
    $pid = $program['f_programId'];
    if( !array_key_exists($pid, $processed) )
    {
        $processed[] = $pid;
        return false;
    }
    return true;
}
foreach( $data as $program ) {
    if (preloop($program))
        continue;

    // do things with program
}
// somewhere else
foreach( $data as $program ) {
    if (preloop($program))
        continue;

    // do things with program
}

【讨论】:

    【解决方案2】:

    您不能在PHP 中执行类似的操作,但要在HTML 中显示数据处理,请使用Twig 或类似的内容。

    【讨论】:

      【解决方案3】:

      我能够通过使用回调得到我想要的。

      首先,定义包装器(“宏”)函数,如下所示:

      function my_macro($data, $fn)
      {
          foreach( $data as $program )
          {
              if( $program['f_enabled'] == 'N' ) continue;
              $pid = $program['f_programId'];
              if( !array_key_exists($pid, $processed) )
              {
                  call_user_func($fn, $program, $pid);
              }
          }
      }
      

      所以在上面,我将$program$pid 传递给一个尚未定义的回调函数,因为这两个变量总是需要并且经常需要。

      要实际使用这种结构,只需这样做:

      my_macro(function($program, $pid)
      {
          /**
           *  Do things here, including
           *  ?>
           *      <span>HTML with <?=$pid?> and <?=$program['title']?></span>
           *  <?php
           */
      });
      

      您可以将其分散到您页面中任何您喜欢的位置。除了$program$pid 之外,您当然可以拥有任意数量的常用变量。

      我仍然建议在问题的 cmets 中阅读 @Marty 的建议,以获得正确的、非 hacky 的方法。但是,是的,这就是我所做的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-04-30
        • 2014-11-21
        • 1970-01-01
        • 2016-08-25
        • 1970-01-01
        • 2018-01-07
        • 1970-01-01
        • 2015-10-29
        相关资源
        最近更新 更多