Using a class in a plugin using $this->
-
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 30Thanks for any help.
Here is the code:
<?php
/*
Plugin Name: mytest
Plugin URI: https://www.mysite.com
Version: v1.00
Author: Eric
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’));?>
- The topic ‘Using a class in a plugin using $this->’ is closed to new replies.