0

I have run into an interesting problem with using static variables in classes and accessing them. In this example I am using PSR-4 loading.

At the top of my file I have the usual

use Networks\ConfigHandlers\ServicesConfig

followed by

class Authenticate{
 ...

function __construct(){

        $this->fullTokenExpireTimeExtend    = ServicesConfig::$timeUnits['halfday'];
        $this->standardTokenExpireTimeExtend= ServicesConfig::$timeUnits['twoday'];
        $this->simpleTokenExpireTimeExtend  = ServicesConfig::$timeUnits['week'];

    }

...
}

In dev this code executes fine without problems. In production however it throws an error:

PHP Fatal error: Class 'Networks\\ConfigHandlers\\ServicesConfig' not found in /var/www/html/public/v1/Authenticate.php on line 151

line 151 contains:

 $this->fullTokenExpireTimeExtend   = ServicesConfig::$timeUnits['halfday'];

from above.

ServicesConfig.php looks like this (Edited down to parts in question):

<? namespace Networks\ConfigHandlers;

/**
 * Configuration.
 * @package xxxxx
 * @author xxxxx
 * @category Config
 * @copyright (c) 2013-2016, xxxxx
 */
class ServicesConfig{

    /**
     * The time units in seconds
     * @var array
     */
    public static $timeUnits = array(
        'second'        => 1,
        'quarterminute' => 15,
        'halfminute'    => 30,
        'minute'        => 60,
        'fiveminutes'   => 300,     // 60*5 Seconds
        'tenminutes'    => 600,     // 60*5*2 Seconds
        'quarterhour'   => 900,     // 60*5*3 Seconds
        'halfhour'      => 1800,    // 60*30 Seconds
        'hour'          => 3600,    // 60*60 seconds
        'halfday'       => 43200,   // 60*60*12 seconds
        'day'           => 86400,   // 60*60*24 seconds
        'twoday'        => 172800,  // 60*60*24*2 seconds
        'week'          => 604800,  // 60*60*24*7 seconds
        'month'         => 2592000, // 60*60*24*30 seconds
        'year'          => 31536000 // 60*60*24*365 seconds
    );

}

What I can't figure out is why this would be happening in production yet works flawlessly in dev. Locally I am running PHP 5.5 on a mac and in production 5.5 on CentOS so I don't believe its a syntactical error due to version, I could be wrong of course.

I checked to make sure the file is in the directory, cross checked names, and it is all as it should be. I have also checked the classmap to make sure it is registered, and it is.

One of the theories I have is that ServicesConfig has to be instantiated first in __construct() before calling ServicesConfig... But if that were the case, wouldn't the same hold true in dev? Have not tried it just yet since My understanding is a little shaky in this area and would rather get a concrete answer to completely understand what and why.

Any explanations for this variation in outcome? I'm stumped.

SeaFuzz
  • 1,177
  • 9
  • 28

1 Answers1

1

@Ryan Vincent was absolutely correct and caught that small detail. I should have known better. Old habits die hard and Sublime makes it too easy to use snippets and forget the details.

Solution: The problem was not in the namespace it was specifically caused by using the php <? short tag instead of the full <?PHP tag.

Thanks for the extra set of eyes.

SeaFuzz
  • 1,177
  • 9
  • 28