[英]Wordpress with PHP Class using init: how to pass variables between functions?
I would like to break apart the function used in Class to make it easier to understand, however I cant seem to work out how to pass variables across between two or more functions while maintaining the WP INIT hook. 我想分解一下Class中使用的函数以使其更容易理解,但是我似乎无法弄清楚如何在保持WP INIT钩子的同时在两个或多个函数之间传递变量。 I have simplified my code for this question.
我已经简化了此问题的代码。
I am not sure if using multiple add_action in __construct is the correct way to go about it. 我不确定在__construct中使用多个add_action是否是正确的方法。
Thanks. 谢谢。
//called from template/index.php
do_action('foo');
//in template/fuctions.php
add_action( 'init', array ( 'foo', 'init' ) );
class foo
{
public $stillnotworking;
public static function init()
{
new self;
}
public function __construct()
{
add_action( 'foo', array ( $this, 'part1' ) );
add_action( 'foo', array ( $this, 'part2' ) );
}
public function part1()
{
$this->x = '123';
$stillnotworking = '123';
}
public function part2()
{
echo $this->x; //not working
echo $stillnotworking;
}
public function __toString()
{
echo $this->x; //not working
echo $stillnotworking;
}
function __destruct() {
}
}
You haven't declare your $x
within you class. 您尚未在课程中声明
$x
。 Try 尝试
add_action( 'init', array ( 'foo', 'init' ) );
class foo
{
public $x;
public $stillnotworking;
public static function init()
{
new self;
}
public function __construct()
{
add_action( 'foo', array ( $this, 'part1' ) );
add_action( 'foo', array ( $this, 'part2' ) );
}
public function part1()
{
$this->x = '123';
$stillnotworking = '123';
}
public function part2()
{
echo $this->x; //not working
echo $stillnotworking;
}
public function __toString()
{
echo $this->x; //not working
echo $stillnotworking;
}
function __destruct() {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.