title: Why does using $this inside a class in a Wordpress plugin throw a fatal error?
body: <p>I'm in the process of writing a Wordpress plugin which creates a page in the admin area, as well as executing some frontend code.</p>
  
  <p>The code below throws a nice <code>Fatal error: Using $this when not in object context</code> error. Which is rather mystifying, as the variable is called inside the class.</p>
  
  <p>Maybe I'm not following the intended Wordpress plugin structure for functions and classes, but the conceptual code below was created using the relevant entries on plugin development in the Wordpress Codex.</p>
  
  <p>Could somebody explain why the error is triggered, because when I create an instance of the class outside of the Wordpress codebase everything is fine.</p>
  
  <pre><code>if (!class_exists("MyClass")) {
    class MyClass {
      var $test = 'Test variable';
  
      public function index() {
        //Index code
      }
  
      public function add() {
        echo $this-&gt;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'));
  }
  </code></pre>