Using the object pattern in a widget (2 posts)

  1. merlinti
    Posted 6 years ago #

    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: http://www.mysite.com
    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'));


  2. goodmast3r
    Posted 6 years ago #

    Hi I got the same problem, any one can help please? Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic