Why does using $this inside a class in a Wordpress plugin throw a fatal error? Why does using $this inside a class in a Wordpress plugin throw a fatal error? wordpress wordpress

Why does using $this inside a class in a Wordpress plugin throw a fatal error?


So, I've seem to have fixed it, by going back to the basics and asking Google the humble question: "Using classes in Wordpress plugins".

Both the article by Jay Fortner and one on dConstructing.com were helpful.

Basically, I'm now calling add_menu_page and add_submenu_page from within the class. I was under the impression those functions somehow created an object, but they obviously don't.

My code now looks something like this and I'm able to call the declared class variable without error:

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;}


This is no longer true.

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.

From: 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')

Or

$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    }}


What you should do is the following:

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'));}

Using array('MyClass', 'index') cause php to execute the method as a static methed, but passing an actual object as the first argument will call the method via the object.

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

Would also work if you want to reuse the object.