我正在学习创建一个WP插件作为一个类。然而,在调用我的函数时,add\\u操作似乎不起作用;它在使用$this->init()时工作。
示例:
class test
{
public $age = NULL;
public function __construct()
{
//register an activation hook
register_activation_hook( __FILE__, array( &$this, \'installplugin\' ) );
add_action(\'init\', array( &$this, \'init\' ) ); //this doesn\'t work
//$this->init(); //This WORKS!
}
function installplugin()
{
add_option( \'age\', 45 );
$this->init();
}
function init()
{
$age = get_option( \'age\' );
$this->age = $age;
}
function printAge( )
{
echo "The age is " . $this->age . "<br />";
}
}
因此,运行后:$learnObj = new learnable_test();
$learnObj->printAge();
它只打印:“年龄是”但是如果我没有注释掉add\\u操作(\'init\',…)然后使用$this->init(),这似乎可行,打印“年龄是45”
我错过了什么?