Title: PHP OOP Shortcode Function
Last modified: August 20, 2016

---

# PHP OOP Shortcode Function

 *  [toneburst](https://wordpress.org/support/users/toneburst/)
 * (@toneburst)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/php-oop-shortcode-function/)
 * 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 1 replies (of 1 total)

 *  Thread Starter [toneburst](https://wordpress.org/support/users/toneburst/)
 * (@toneburst)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/php-oop-shortcode-function/#post-3215859)
 * Sorry, posted in the wrong forum.
    Please ignore…
 * a|x
 * _[continued here: [http://wordpress.org/support/topic/oop-shortcode-function-and-property-types-2?replies=1](http://wordpress.org/support/topic/oop-shortcode-function-and-property-types-2?replies=1)]_

Viewing 1 replies (of 1 total)

The topic ‘PHP OOP Shortcode Function’ is closed to new replies.

## Tags

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

 * 1 reply
 * 1 participant
 * Last reply from: [toneburst](https://wordpress.org/support/users/toneburst/)
 * Last activity: [13 years, 6 months ago](https://wordpress.org/support/topic/php-oop-shortcode-function/#post-3215859)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
