2011-02-18 57 views
7

我正在编写一个Wordpress插件,它在管理区域创建一个页面,并执行一些前端代码。

下面的代码会抛出一个不错的Fatal error: Using $this when not in object context错误。这很神秘,因为这个变量在课堂内部被调用。

也许我没有按照Wordpress插件结构的函数和类,但下面的概念代码是使用Wordpress Codex中插件开发的相关条目创建的。

有人可以解释为什么错误被触发,因为当我在Wordpress代码库之外创建类的实例时,一切都很好。

if (!class_exists("MyClass")) { 
    class MyClass { 
    var $test = 'Test variable'; 

    public function index() { 
     //Index code 
    } 

    public function add() { 
     echo $this->test; 
    } 
    } 
} 

add_action('admin_menu', 'my_plugin_menu'); 

function my_plugin_menu() { 
    add_menu_page('My Plugin', 'My Plugin', 'manage_options', 'my-plugin', array('MyClass', 'index')); 
    add_submenu_page('my-plugin', 'Add New Thing', 'Add New', 'manage_options', 'my-plugin-add', array('MyClass', 'add')); 
} 

回答

9

所以,我似乎已经修复了它,回到基础并询问谷歌的一个简单问题:“在Wordpress插件中使用类”。

Jay Fortner的文章和dConstructing.com的文章都很有帮助。

基本上,我现在在类中调用add_menu_page和add_submenu_page。我以为这些功能创造了一个对象,但他们显然没有。

我的代码现在看起来是这样的,我能够调用声明类变量没有错误:

if (!class_exists("MyClass")) { 
    class MyClass { 
    var $test = 'Test variable'; 

    function __construct() { 
     add_action('admin_menu', 'my_plugin_menu'); 
    } 

    function my_plugin_menu() { 
     add_menu_page('My Plugin', 'My Plugin', 'manage_options', 'my-plugin', array(&$this, 'index')); 
     add_submenu_page('my-plugin', 'Add New Thing', 'Add New', 'manage_options', 'my-plugin-add', array(&$this, 'add')); 
    } 

    public function index() { 
     //Index code 
    } 

    public function add() { 
     echo $this->test; 
    } 
    } 
    new MyClass; 
} 
+0

嘿...,代码不适合我。所以,我改变这个`add_action('admin_menu','my_plugin_menu');`这个`add_action('admin_menu',array($ this,'my_plugin_menu'));` – KeepMove 2014-06-12 15:32:08

1

取决于类是如何被调用的,静态的Class :: method()会抛出错误。如果是这种情况,我认为你需要使用self :: $ test;但可能是错误的。

+0

我不知道WordPress的是如何调用的类,但使用自::测试,而不是$这个 - >测试不能解决错误,恐怕。 – mensch 2011-02-18 12:24:51

4

你应该做的是:

function my_plugin_menu() 
{ 
add_menu_page('My Plugin', 'My Plugin', 'manage_options', 'my-plugin', array(new MyClass, 'index')); 
add_submenu_page('my-plugin', 'Add New Thing', 'Add New', 'manage_options', 'my-plugin-add', array('MyClass', 'add')); 
} 

使用array('MyClass', 'index')原因PHP以静态方式执行该方法,但传递实际对象作为第一个参数将通过该对象调用该方法。

function my_plugin_menu() 
{ 
    $Class = new MyClass(); 
    add_menu_page(
     'My Plugin', 
     'My Plugin', 
     'manage_options', 
     'my-plugin', 
     array($Class, 'index') 
    ); 
} 

如果您想重用该对象,也可以使用。

4

这不再是事实。

Don't forget, if you pass in the class, you may want to pass it by reference, you can even pass a classes own functions using &$this inside of a class and continue to modify the same instance, this helps to not have to recreate a class everytime you call a different part of the plugin but the same class.

来源:https://codex.wordpress.org/Function_Reference/do_action_ref_array

As of PHP 5.4, the array is no longer passed by reference despite the function's name. You cannot even use the reference sign '&' because call time pass by reference now throws an error. What you can do is pass the reference pointer as an array element. Doing so does require all callbacks added to the action to expect a reference pointer. This is not something you will see in WordPress actions. This technique is provided for informational purposes only.

$Class = new MyClass(); 
add_menu_page(
     'My Plugin', 
     'My Plugin', 
     'manage_options', 
     'my-plugin', 
     array(&$Class, 'index') 

或者

$myClass= new MyClass ; 

add_action('admin_menu', array($myClass, 'admin_menu')); 

class MyClass 
{ 
    public function admin_menu() 
    { 
     add_menu_page('MyMenu', 'MyMenu', 'read', 'mymenu', array($this, 'action')); 
    } 
    public function action() 
    { 
     //Do something here 
    } 
} 
+0

内部动作我添加了表单标签。那么,我应该通过什么行动,我可以在哪里获得发布价值?提前致谢。 – 2016-12-09 12:39:44

相关问题