我正在制作一个带有OOP概念的WordPress插件,而我正面临一些有线问题。
首先我有一个main-plugin.php
文件,我有一个像这样的课程
include_once plugin_dir_path( __FILE__ ) . 'something.php';
class something {
//some var
public $a;
private function __construct() { {
//all of my wordpress hook calls are here
}
public function foo() {
$this->a = 55; // this is working fine and not giving any error
}
}
现在我有另一个php文件,它包含我在main-plugin.php
文件中包含的另一个类
我们说新文件是something.php
我在这里
class some {
//some variables
public $b, $c;
// now this this class when I'm doming
public function bar() {
$this->b =1; // this is giving me error saying this is not an object.
}
}
现在我不知道为什么$this
在else类中没有工作。另外由于这些变量不是静态变量,我真的不能self::$b
。所以,我希望是否有其他人可以告诉我如何在不使用$this
的情况下访问类中的非静态变量。
答案 0 :(得分:1)
我认为,您必须在https://github.com/isaumya/adsense-invalid-click-protector/blob/master/adsense-invalid-click-protector.php#L91中使用AICP_ADMIN
的实例而不是字符串。所以尝试像
$aicpAdmin = new AICP_ADMIN;
add_action( 'admin_enqueue_scripts', array( $aicpAdmin, 'admin_scripts' ) );
答案 1 :(得分:0)
$this
仅在类中的函数内可用。如果要初始化变量,请使用construct function:
public class test
{
public $a;
public function __construct()
{
$this->a = new db();
}
}
使用test
初始化课程$test = new test()
时,$test->a
将成为db
的实例。