Why might a static variable I've initialized in a class become unset? Here is class( is 'static' ):
class Events {
// static events collection
private static $events = null;
// private(unused) constructor
private function __construct() {}
// initializes the class
public static function initialize($events){
self::$events = $events;
}
// returns the event collection
public static function get(){
return self::$events;
}
}
I set $events like this:
functions.php:
include_once "events.php";
// a function to initialize the application
function init(){
// retrieve events from database
$events = get_events_from_server();
// initialize the events class
Events::initialize($events);
}
init() is called from index.php. The variable seems to be unset after index.php has completely loaded. I post from javascript to a server page: get_events.php to request the list of json encoded events. At get_events.php the static events variable is null. Here is get_events.php:
<?php
include_once "../ccc-psl-config.php";
include_once WEBROOT ."/includes/functions.php";
include_once WEBROOT ."/includes/db_connect.php";
include_once WEBROOT ."/types/events.php";
ob_start();
try{
// open database connection
$pdo = DatabaseConnection::open();
$events = Events::get();
//$events = get_events($pdo);
if($events){
echo $events;
}
else {
echo false;
}
}
catch(Exception $e){
print_r("Failed to get events from database: ".$e.getMessage());
echo false;
}
// close database connection
DatabaseConnection::close();
ob_end_flush();