Title: OOP, Shortcode Function and Property Types
Last modified: August 20, 2016

---

# OOP, Shortcode Function and Property Types

 *  [toneburst](https://wordpress.org/support/users/toneburst/)
 * (@toneburst)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/oop-shortcode-function-and-property-types-2/)
 * Hi,
 * I’m struggling the PHP OOP concepts, trying to make a shortcode plugin.
    Here’s
   my very simple shortcode class, so far:
 *     ```
       class ell_jt_shortcode {
       	static $defs = NULL;
       	static $addscripts = false;
   
       	function __construct() {
       		///////////////////
       		// Add shortcode //
       		///////////////////
       		add_shortcode( 'ell-jtweets', array( __CLASS__, 'handle_shortcode' ) );
       	}
       	function handle_shortcode( $atts ) {
       		return var_dump(self::$addscripts);
       	}
       }
       $jtshortcode = new ell_jt_shortcode();
       ```
   
 * This seems to work OK, but I’m wondering why I only seem to be able to access
   static variables inside the shortcode functions. From reading OOP tutorials, 
   I was assuming that I could use a private or protected variable for $addscript,
   like so:
 *     ```
       class ell_jt_shortcode {
       	static $defs = NULL;
       	private $addscripts = false;
   
       	function __construct() {
       		///////////////////
       		// Add shortcode //
       		///////////////////
       		add_shortcode( 'ell-jtweets', array( __CLASS__, 'handle_shortcode' ) );
       	}
       	function handle_shortcode( $atts ) {
       		return var_dump($this->addscripts);
       	}
       }
       $jtshortcode = new ell_jt_shortcode();
       ```
   
 * But this produces a fatal “Using $this when not in object context in PHP” error.
   Is this as expected? Is there some other way I should go about creating a PHP
   class for my shortcode. Is using a static variable in this context OK? I’m using
   a static for the default settings, but I think this is OK, as they’re always 
   going to be the same.
 * I’m still struggling with basic OOP concepts, I must admit. I’ve been writing
   procedural PHP for quite a long time, but am trying update myself, finally. It’s
   quite hard though, as a completely self-taught code-tinkerer.
 * a|x

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [Chris](https://wordpress.org/support/users/zenation/)
 * (@zenation)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/oop-shortcode-function-and-property-types-2/#post-3216096)
 * Try using &$this instead of __CLASS__
 * `add_shortcode( 'ell-jtweets', array( &$this, 'handle_shortcode' ) );`
 * __CLASS__ only refers to the class name not the instance.
 *  Thread Starter [toneburst](https://wordpress.org/support/users/toneburst/)
 * (@toneburst)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/oop-shortcode-function-and-property-types-2/#post-3216097)
 * Ah, thanks Chris. That makes sense. I copies that part from a tutorial where 
   a static class was used.
 * Thanks again,
 * a|x
 *  Thread Starter [toneburst](https://wordpress.org/support/users/toneburst/)
 * (@toneburst)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/oop-shortcode-function-and-property-types-2/#post-3216101)
 * That does it- thanks, Chris!
 * a|x
 *  [Chris](https://wordpress.org/support/users/zenation/)
 * (@zenation)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/oop-shortcode-function-and-property-types-2/#post-3216102)
 * You’re welcome, a|x 🙂 Glad I could help
 *  [Barry](https://wordpress.org/support/users/barryhughes/)
 * (@barryhughes)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/oop-shortcode-function-and-property-types-2/#post-3216113)
 * Just to chip in: I think dropping the ampersand would be preferable, certainly
   with PHP 5.0 or greater:
 * `array($this, 'shortcode')`

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘OOP, Shortcode Function and Property Types’ is closed to new replies.

## Tags

 * [oop](https://wordpress.org/support/topic-tag/oop/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 5 replies
 * 3 participants
 * Last reply from: [Barry](https://wordpress.org/support/users/barryhughes/)
 * Last activity: [13 years, 4 months ago](https://wordpress.org/support/topic/oop-shortcode-function-and-property-types-2/#post-3216113)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
