Search code examples
phpstaticpublicstaging

static public class members in PHP on staging server


Our staging server is generating a parse error and I'm wondering if our pre-staging server has been letting our dev team get away with incorrect syntax.

Here's the code causing the 'parse error':

       // GLOBALS.PHP
  <?php
       session_start();

      class ItemsFromBronzeAge  {
               static public $ITEMNAMELABEL = "ItemName";
      }

      class Labels {
                   static public $USER_PROMPT_ITEMNAME = "Item name here....";
      }
   ?>

We include the above globals.php into the 2nd file -- called index.php -- like this:

             // INSIDE OF INDEX.PHP
      <?php
        require_once 'globals.php'; // variables and statics used throughout

             // the next line is line #12 in the 'Parse error' message below
        $_SESSION[ItemsFromBronzeAge::$ITEMNAMELABEL] = Labels::$USER_PROMPT_ITEMNAME;

      ?>

And here's the parse error when we run index.php on our 1and1.com staging server:

       " Parse error: syntax error, unexpected ']', 
         expecting '(' in index.php on line 12"

Just to drive this home, we changed line #12 in the code in index.php to this and the 1and1.com staging server no longer reports a 'parse error':

    // WE CHANGED FROM THIS:
    $_SESSION[ItemsFromBronzeAge::$ITEMNAMELABEL] = Labels::$USER_PROMPT_ITEMNAME;

    // TO THIS AND THE 'PARSE ERROR' GOES AWAY -- THIS CODE WORKS ON THE STAGING SERVER:
    $_SESSION["ItemName"] = "Item name here....";

There is NO WAY our team is going back through all our source code, dropping the use of constants for array indexes and regressing to use string literals as array indexes.

My question is -- has our pre-staging dev server been letting us get away with incorrect syntax above, or does 1and1.com have some sort of PHP setting out of whack, or.......?

I mean the use of public static class members as array indexes -- is commonplace.


Solution

  • What version of PHP are you running on both systems?

    It sounds like it may be related to different versions of PHP.