Support » Plugins » Using the object pattern in a widget

  • Hi all,

    I’m trying to create a basic widget. I’m using a php class and when using $this->varName; it’s giving me an error like it doesn’t know it’s in an object. Here is the error:
    Fatal error: Using $this when not in object context in /Applications/MAMP/htdocs/wordpress1/wp-content/plugins/mytest/mytest.php on line 30

    Thanks for any help.
    Here is the code:
    Plugin Name: mytest
    Plugin URI:
    Version: v1.00
    Author: Eric
    Description: does a test


    class mytestClass {

    public $lastChecked;

    public function __construct() {

    $this->lastChecked=date(‘n-j-Y g:i:s’, time());


    public function control(){
    echo ‘I am a control panel’;

    public function widget($args){
    echo $args[‘before_widget’];
    echo $args[‘before_title’] . ‘Test Widget’ . $args[‘after_title’];
    echo ‘I am your widget’;
    echo $this->lastChecked;
    echo $args[‘after_widget’];

    public function register(){
    register_sidebar_widget(‘mytestClass’, array(‘mytestClass’, ‘widget’));
    register_widget_control(‘mytestClass’, array(‘mytestClass’, ‘control’));


    $mytestClass=new mytestClass();
    add_action(“widgets_init”, array($mytestClass, ‘register’));


Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Using the object pattern in a widget’ is closed to new replies.