Title: Errors when using public variables in PHP classes in WordPress plugins
Last modified: August 18, 2016

---

# Errors when using public variables in PHP classes in WordPress plugins

 *  [numbersix](https://wordpress.org/support/users/numbersix/)
 * (@numbersix)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/errors-when-using-public-variables-in-php-classes-in-wordpress-plugins/)
 * Hi all,
 * Apologies in advance for the level of detail in this post – it’s something I’ve
   been working on for ages so I want to cover all possibilities now if I can. So…
 * I’m trying to write a plugin that performs functions on Facebook. For various
   reasons one of the requirements is that I have to reference Facebook classes 
   provided in the Facebook client libraries.
 * The Facebook client libraries make extensive use of PHP classes, for example :
 *     ```
       class FacebookRestClient {
         public $secret;
         public $session_key;
         public $api_key;
         public $friends_list; // to save making the friends.get api call, this will get prepopulated on canvas pages
         public $added;        // to save making the users.isAppAdded api call, this will get prepopulated on canvas pages
       ...
       }
       ```
   
 * The above class is one of the Facebook classes and, as you can see, has a number
   of public variables defined.
 * When I try to do this and try to activate my plugins I get an error saying “Plugin
   could not be activated because it triggered a fatal error.”
 * After literally hours of trying to figure out why this is we seem to have tracked
   the problem down to the keyword ‘public’ in the class above. If I create an empty
   class like the one below (which obviously doesn’t do anything) the plugin gets
   activated nicely.
 *     ```
       class FacebookRestClient
       {
       }
       ```
   
 * Add `public $secret` to that class though and the plugin fails to activate.
 * The Facebook client libraries don’t work properly without these classes being
   defined, the end result being that I can’t make proper use of them within my 
   WordPress plugin.
 * Does anyone know why this might be? The PHP syntax above is fine as it’s the 
   documented way of doing things. It just won’t work inside WordPress plugins.
 * ANY help would be much appreciated – I have less hair now than when I started
   writing this thing. 🙂
 * Thanks!
    NumberSix

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

 *  Thread Starter [numbersix](https://wordpress.org/support/users/numbersix/)
 * (@numbersix)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/errors-when-using-public-variables-in-php-classes-in-wordpress-plugins/#post-590853)
 * Further to this it seems to be when using ANY variable outside the scope of a
   local fuction. E.g. the following class declaration works fine.
 *     ```
       class FacebookRestClient
       {
   
          function doSomeStuff()
          {
              $secret = "";
          } 
   
       }
       ```
   
 * But this doesn’t.
 *     ```
       class FacebookRestClient
       {
   
          $secret = "";
   
       }
       ```
   
 *  Thread Starter [numbersix](https://wordpress.org/support/users/numbersix/)
 * (@numbersix)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/errors-when-using-public-variables-in-php-classes-in-wordpress-plugins/#post-590854)
 * Hehe and further yet AGAIN … this works :
 *     ```
       class FacebookRestClient {
         var $secret;
         var $session_key;
         var $api_key;
         var $friends_list; // to save making the friends.get api call, this will get prepopulated on canvas pages
         var $added;        // to save making the users.isAppAdded api call, this will get prepopulated on canvas pages
   
         function __construct($api_key, $secret, $session_key=null) {
         }
       }
       ```
   
 * but this doesn’t :
 *     ```
       class FacebookRestClient {
         var $secret;
         var $session_key;
         var $api_key;
         var $friends_list; // to save making the friends.get api call, this will get prepopulated on canvas pages
         var $added;        // to save making the users.isAppAdded api call, this will get prepopulated on canvas pages
   
         public function __construct($api_key, $secret, $session_key=null) {
         }
       }
       ```
   
 * The only difference between them is the use of the word `public` in the __construct
   declaration …
 *  Thread Starter [numbersix](https://wordpress.org/support/users/numbersix/)
 * (@numbersix)
 * [18 years, 11 months ago](https://wordpress.org/support/topic/errors-when-using-public-variables-in-php-classes-in-wordpress-plugins/#post-590989)
 * Fixed it.

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

The topic ‘Errors when using public variables in PHP classes in WordPress plugins’
is closed to new replies.

## Tags

 * [class](https://wordpress.org/support/topic-tag/class/)
 * [classes](https://wordpress.org/support/topic-tag/classes/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [public](https://wordpress.org/support/topic-tag/public/)
 * [variables](https://wordpress.org/support/topic-tag/variables/)

 * 3 replies
 * 1 participant
 * Last reply from: [numbersix](https://wordpress.org/support/users/numbersix/)
 * Last activity: [18 years, 11 months ago](https://wordpress.org/support/topic/errors-when-using-public-variables-in-php-classes-in-wordpress-plugins/#post-590989)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
