如何循环遍历包含函数变量的PHP类

如何循环遍历包含函数变量的PHP类,php,wordpress,Php,Wordpress,目标:我的目标是创建一个执行多次并向页面页脚添加ascript的函数 这是可行的,但现在我希望它能处理变量。我如何将它们放入第二个函数($A、$B、$C)并为它们提供输入? 这是我的代码: class myClass { //Put it in the footer public function __construct() { add_action( 'genesis_after_footer', array( $this, 'myFunction' ) );

目标:我的目标是创建一个执行多次并向页面页脚添加ascript的函数

这是可行的,但现在我希望它能处理变量。我如何将它们放入第二个函数($A、$B、$C)并为它们提供输入? 这是我的代码:

class myClass {
    //Put it in the footer
    public function __construct() {
        add_action( 'genesis_after_footer', array( $this, 'myFunction' ) );
    }


    //myFunction
     public function myFunction($A, $B, $C) {

    echo'<script>
     $(document).ready(function() {


      $("#gallery_'.$A.'").owlCarousel({

          autoPlay: false, //Set AutoPlay to 3 seconds

          items : '.$B.',
          itemsDesktop : [1199,'.$B.'],
          itemsDesktopSmall : [979,'.$B.'],
          itemsTablet: [768,'.$C.']

      });

    });
    </script>';

    }
}
我想要一些像:

$run = new WP_Docs_Class($A, $B, $C);
or
$run = new WP_Docs_Class(120, 9, 2);

非常感谢您的帮助

只需将它们存储到类变量中即可

class myClass {
    private $A, $B, $C;
    //Put it in the footer
    public function __construct($A, $B, $C) {
        $this->A = $A;
        $this->B = $B;
        $this->C = $C;
        add_action( 'genesis_after_footer', array( $this, 'myFunction' ) );
    }


    //myFunction
     public function myFunction() {

    echo'<script>
     $(document).ready(function() {


      $("#gallery_'.$this->A.'").owlCarousel({

          autoPlay: false, //Set AutoPlay to 3 seconds

          items : '.$this->B.',
          itemsDesktop : [1199,'.$this->B.'],
          itemsDesktopSmall : [979,'.$this->B.'],
          itemsTablet: [768,'.$this->C.']

      });

    });
    </script>';

    }
}
class-myClass{
私人$A、$B、$C;
//把它放在页脚
公共函数构造($A、$B、$C){
$this->A=$A;
$this->B=$B;
$this->C=$C;
添加_操作('genesis_后面的_footer',数组($this,'myFunction'));
}
//我的功能
公共函数myFunction(){
回声'
$(文档).ready(函数(){
$(“#画廊.'$this->A.”)。猫头鹰旋转木马({
自动播放:false,//将自动播放设置为3秒
项目:'.$this->B',
itemsDesktop:[1199'。$this->B.],
itemsDesktopSmall:[979'。$this->B.],
itemsTablet:[768'。$this->C.]
});
});
';
}
}
试试这个;)

在WP中,我们使用


你还没有完成什么?请自学基于类的面向对象编程的基础知识。你缺乏这方面的知识将阻碍你做任何事情。它是否与
$run=new WP_Docs_Class()一起工作
如预期的那样?如果是,您确定它会工作吗?请您解释一下。传递给
add_action
的回调是成员类的一个方法,这就是为什么他必须传递
$this
作为将在其上执行的对象引用的原因。从这个意义上说,您可以访问类成员变量see Usage for Hi,谢谢您的帮助!最低点的解决方案非常有效。我也尝试过测试这个函数,但我不知道如何多次钩住这个函数。它是否也适用于
$run=newmyclass(120,9,2)
class myClass {
    private $A, $B, $C;
    //Put it in the footer
    public function __construct($A, $B, $C) {
        $this->A = $A;
        $this->B = $B;
        $this->C = $C;
        add_action( 'genesis_after_footer', array( $this, 'myFunction' ) );
    }


    //myFunction
     public function myFunction() {

    echo'<script>
     $(document).ready(function() {


      $("#gallery_'.$this->A.'").owlCarousel({

          autoPlay: false, //Set AutoPlay to 3 seconds

          items : '.$this->B.',
          itemsDesktop : [1199,'.$this->B.'],
          itemsDesktopSmall : [979,'.$this->B.'],
          itemsTablet: [768,'.$this->C.']

      });

    });
    </script>';

    }
}
class myClass{

  //Put it in the footer
  public function __construct(){
    add_action('genesis_after_footer', array(
      $this,
      'myFunction'));
  }

  //myFunction
  public function myFunction($data){

    echo'<script>
     $(document).ready(function() {
      $("#gallery_' . $data[0] . '").owlCarousel({
          autoPlay: false, //Set AutoPlay to 3 seconds
          items : ' . $data[1] . ',
          itemsDesktop : [1199,' . $data[1] . '],
          itemsDesktopSmall : [979,' . $data[1] . '],
          itemsTablet: [768,' . $data[2] . ']
      });
    });
    </script>';
  }

}
do_action( 'genesis_after_footer', array( $A, $B, $C ) );
do_action( 'genesis_after_footer', array( 120, 9, 2 ) );