Title: Using the object pattern in a widget
Last modified: August 19, 2016

---

# Using the object pattern in a widget

 *  [merlinti](https://wordpress.org/support/users/merlinti/)
 * (@merlinti)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/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: <?php /* Plugin Name: mytest Plugin URI:
   [http://www.mysite.com](http://www.mysite.com) Version: v1.00 Author: [Eric](http://www.mysite.com)
   Description: does a test
 * */
    error_reporting(E_ALL);
 * 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)

 *  [goodmast3r](https://wordpress.org/support/users/goodmast3r/)
 * (@goodmast3r)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/using-the-object-pattern-in-a-widget/#post-1044655)
 * Hi I got the same problem, any one can help please? Thanks

Viewing 1 replies (of 1 total)

The topic ‘Using the object pattern in a widget’ is closed to new replies.

## Tags

 * [object](https://wordpress.org/support/topic-tag/object/)
 * [widget](https://wordpress.org/support/topic-tag/widget/)

 * 1 reply
 * 2 participants
 * Last reply from: [goodmast3r](https://wordpress.org/support/users/goodmast3r/)
 * Last activity: [16 years, 8 months ago](https://wordpress.org/support/topic/using-the-object-pattern-in-a-widget/#post-1044655)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
