WordPress.org

Ready to get started?Download WordPress

Forums

Errors when using public variables in PHP classes in WordPress plugins (4 posts)

  1. NumberSix
    Member
    Posted 7 years ago #

    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

  2. NumberSix
    Member
    Posted 7 years ago #

    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 = "";
    
    }
  3. NumberSix
    Member
    Posted 7 years ago #

    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 ...

  4. NumberSix
    Member
    Posted 7 years ago #

    Fixed it.

Topic Closed

This topic has been closed to new replies.

About this Topic