静态变量填满,后来突然清空?

时间:2017-05-29 16:42:20

标签: php static static-variables

我遇到了一个问题,我设置了一个静态变量(我只在其定义的类中访问),但下次同一个类中的方法(所有方法都是静态顺便说一句),它突然变为null 。我已经检查过了,没有方法可以访问这个变量。有什么我想念的吗?

class Foo {
    private static $var;

    public static function setTest($a) {
         self::$var = $a;
    }

    public static function getTest() {
         return self::$var;
    }
}

- - - - - - - - 编辑 这是我正在研究的实际课程:

    <?php
/**
 * Class Cache
 */
class Cache
{
    /**
     * @var
     */
    private static $cachedir;
    private static $checked = true;
    /**
     * @param $file_contents
     * @param string $class
     */
    public static function add($file_contents, $class = "", $method = "")
    {
        if (empty($class)) {
            $trace = debug_backtrace();
            if (!empty($trace[1])) {
                $class = $trace[1]["class"];
                $method = $trace[1]["function"];
            }
        }
        $cachedir = self::getDir();
        \Dir::make($cachedir . "/" . $class);
        \File::put($cachedir . "/" . $class, $method . ".cache", base64_encode(serialize($file_contents)));
    }
    /**
     * @return mixed
     */
    public static function getDir()
    {
        $cachedir = self::$cachedir;
        if (empty($cachedir)) {
            $cachedir = self::init();
        }
        return $cachedir;
    }
    /**
     * @param mixed $cachedir
     */
    public static function setDir($cachedir)
    {
        self::$cachedir = $cachedir;
    }
    /**
     *
     */
    public static function init()
    {
        $user = \Auth::user();
        if (empty($user)) {
            return false;
        }
        $cachedir = "cache/" . session_id() . "-" . $user["uid"];
        if (!\File::exists($cachedir . "/Cache/create_date.cache")) {
            self::setDir($cachedir);
            \Dir::make($cachedir);
            self::add(new \DateTime(), "Cache", "create_date");
            if (!\File::exists(APP_PATH . "/" . $cachedir . "/Env/environment.cache")) {
                self::add(\Env::getSettings(), "Env", "environment");
            }
        } elseif (self::$checked) {
//            $created_at = self::get("Cache", "create_date");
//            self::$checked = true;
//            dd($created_at);
        }
        return $cachedir;
    }
    /**
     * @param string $class
     * @param string $method
     * @return mixed
     */
    public static function get($class = "", $method = "")
    {
        if (empty($class)) {
            $trace = debug_backtrace();
            if (!empty($trace[1])) {
                $class = $trace[1]["class"];
                $method = $trace[1]["function"];
            }
        }
        $cachedir = self::getDir();
        if (!empty($cachedir)) {
            $path = APP_PATH . "/" . $cachedir . "/" . $class . "/" . $method . ".cache";
            if (\File::exists($path)) {
                $file = \File::get($path);
                return unserialize(base64_decode($file));
            }
        }
        return false;
    }
}

0 个答案:

没有答案