【发布时间】:2017-01-13 12:19:18
【问题描述】:
首先:
在 main.php 上,我可以使用 $this->tester1 在函数之间共享变量,因此我需要 public $tester1;在我的文件顶部 - 没有它似乎可以正常工作?
其次:
我正在尝试获取 other.php 上 main.php 中设置的变量,如果我从函数中删除 $this->tester1 = 'test'; 并设置 public $tester1 to = 'test'; 然后 @任何一个文件上的 987654324@ 都可以正常输出。
但是,我需要的变量不是“测试”,而是 = get_option( 'my_option' );,它需要在 init 操作上的 __construct 之后调用,因此需要在函数 mymainfunction 内。
如果我尝试在该函数中设置变量并尝试通过 other.php 访问它,它会显示 unexpected PUBLIC,如果我从变量中删除 public 则输出是 未定义的属性: MyMainClass::$tester1
get_option 的输出是一行文本。
我想我几乎可以做到这一点,但缺少一些简单的东西——希望如此! :)
这是我的文件:
main.php
class MyMainClass {
public $tester1;
public function __construct() {
add_action( 'init', array( &$this, 'mymainfunction' ) );
}
public function mymainfunction() {
$this->tester1 = 'test';
echo $this->tester1;
}
}
other.php
class MyOtherClass {
public function __construct() {
add_action( 'init', array(&$this, 'myotherfunction' ) );
}
public function myotherfunction() {
require_once 'main.php';
$getvariables = new MyMainClass();
echo 'tester1 is:' . $getvariables->tester1;
}
}
【问题讨论】: