如何在PHP应用程序中保留数据

时间:2015-04-09 22:25:14

标签: php

为什么我在类中初始化的静态变量可能会被取消?这是类(是'静态'):

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;
    }
}

我设置了这样的$ events:

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);
}

从index.php调用init()。 index.php完全加载后,变量似乎未设置。我从javascript发布到服务器页面:get_events.php请求json编码事件列表。在get_events.php中,static events变量为null。这是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();

0 个答案:

没有答案