【问题标题】:How do I create a function inside of a foreach loop php?如何在 foreach 循环 php 中创建函数?
【发布时间】:2018-01-14 22:27:06
【问题描述】:

有一个名为 ninja forms 的 wordpress 插件,http://developer.ninjaforms.com/codex/merge-tags/

        /* Individual tag registration. */
        $this->merge_tags = array(

            'foo' => array(
              'id' => 'foo',
              'tag' => '{my:foo}', // The tag to be  used.
              'label' => __( 'Foo', 'my_plugin' ), // Translatable label for tag selection.
              'callback' => 'foo' // Class method for processing the tag. See below.
          ),
        );

        /*
         * Use the `init` and `admin_init` hooks for any necessary data setup that relies on WordPress.
         * See: https://codex.wordpress.org/Plugin_API/Action_Reference
         */
        add_action( 'init', array( $this, 'init' ) );
        add_action( 'admin_init', array( $this, 'admin_init' ) );
      }

      public function init(){ /* This section intentionally left blank. */ }
      public function admin_init(){ /* This section intentionally left blank. */ }

      /**
       * The callback method for the {my:foo} merge tag.
       * @return string
       */
      public function foo()
      {
        // Do stuff here.
        return 'bar';

} }

'callback' 的值然后用作函数,公共函数(foo)。

我已将此添加到数组中:

[foo] => Array
    (
        [id] => foo
        [tag] => {my:foo}
        [label] => Foo
        [callback] => foo
    )

[surveyid] => Array
    (
        [id] => surveyid
        [tag] => {my:surveyid}
        [label] => Surveyid
        [callback] => surveyid
    )

[membername] => Array
    (
        [id] => membername
        [tag] => {my:membername}
        [label] => Membername
        [callback] => membername
    )

我已经向这个数组添加了更多具有相同格式的数组,并且我喜欢将它们的“回调”值设置为公共函数。

 /**
   * The callback method for the {my:foo} merge tag.
   * @return string
   */
  public function foo()
  {
    // Do stuff here.
    return 'bar';
  }

虽然我计划多次这样做,但将来我可能会添加更多数组。所以我试图为每个数组回调值动态分配公共函数。

这就是我所拥有的。

        $data = array(

        '@attributes' => array(
            'surveyid' => 'V54236',
            'membername' => 'John Smith',

            ));

    $realThing = array();
    foreach($data['@attributes'] as $key => $value) {    
        $realThing[$key] = array(
            'id' =>  $key,
            'tag' => '{my:'.$key.'}', 
            'label' => __( ucfirst($key), 'my_plugin' ),
            'callback' => $key

   );
}

    $this->merge_tags = $realThing;

        add_action( 'init', array( $this, 'init' ) );
        add_action( 'admin_init', array( $this, 'admin_init' ) );
      }

      public function init(){ /* This section intentionally left blank. */ }
      public function admin_init(){ /* This section intentionally left blank. */ }

    }

我尝试为每个回调值分配函数。

        foreach($realThing as $key => $value){
             public function $key['callback'](){
                 return $data['@attributes'][$key];
             }
         };

想要的输出:

  public function foo()
  {
    // Do stuff here.
    return 'bar';
  }

  public function surveyid()
  {
    // Do stuff here.
    return 'V54236';

  public function membername()
  {
    // Do stuff here.
    return 'John Smith';

感谢所有帮助。

还得到:语法错误,意外的“foreach”(T_FOREACH),期望函数(T_FUNCTION)在

【问题讨论】:

  • 我想理论上你可以在循环过程中创建一个匿名函数。更大的问题是你为什么要这样做?这种聪明的alec编码很快就会导致无法维护的混乱。

标签: php arrays wordpress ninja-forms


【解决方案1】:

你在处理数据时有一些错误,你应该只声明每个函数一次:

foreach($realThing as $key => $value){
         if(!function_exists($value['callback'])){
             public function $value['callback'](){
                 return $data['@attributes'][$key];
             }
          }
 };

但是,此代码不起作用,因为函数声明中不允许使用变量。以下代码应该可以工作,但是您必须以不同的方式调用回调,因为它存储在数组中:

$callbacks = array();
foreach($realThing as $key => $value){
         if(!isset($callbacks[$value['callback']])){
             $callbacks[$value['callback']] = function () use ($data, $key){
                 return $data['@attributes'][$key];
             };
          }
 };
 unset($key);

 echo $callbacks["surveyid"]();

我很确定你可以通过其他方式做你想做的事。

【讨论】:

  • 语法错误,意外的 '$key' (T_VARIABLE),在此处的最后一行:`function $key['callback'](){`
  • 意外的'$callbacks'(T_VARIABLE),期待函数(T_FUNCTION),如果它需要一个函数,我应该如何在类内部声明函数。 /** * {my:foo} 合并标签的回调方法。 * @return 字符串 */ $callbacks = array(); foreach($realThing as $key => $value){ if(!isset($callbacks[$value['callback']])){ $callbacks[$value['callback']] = function () 使用 ($数据,$key){ return $data['@attributes'][$key]; }; }
  • 不确定你想要什么,你可以用echo $callbacks["surveyid"]();来调用这个函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-01
  • 1970-01-01
  • 2018-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-30
相关资源
最近更新 更多