2011-02-18 55 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

相關問題