为什么我的getter(__get)没有在PHPclass中调用?

Why my getter (__get) is not called in PHP class?

我已阅读所有相关问题,但未能从我的代码中删除错误。请指导我了解我的代码中可能存在的错误。 当我尝试调用以下代码时,它报告 Error: Call to undefined method SessionManager::close() in E:\wamp64\www\mjs-cms\private\systemcore\helper\SessionManager.php on line 22 而不是 "tried to call close".

提前致谢。

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class SessionManager{
    public function __construct() {
        session_start();
    }
    public function is_exist($a){
        return isset($_SESSION["system".$a]);
    }

    public function add($a,$b){
        $_SESSION["system".$a]=$b;
    }
    public function  addCookies($a,$b){
        setcookie($a, $b, time() + (86400 * 30), "/"); // 86400 = 1 day
    }
    public function sessionKey(){
        return session_id();
    }
    public function value($k){
        if(!isset($_SESSION[$k]))
            $this->close("SESSION_NOT_DEFINED".__LINE__);
        return $_SESSION[$k];
    }
    public function __get($key)
    {
        echo "tried to call $key";
        return get_instance()->$key;
    }
}

__get 方法用于访问 class 的未声明属性。

调用未声明的函数是__call or __callStatic

public function __call($method_name, $arguments)
{
    echo "tried to call: $method_name";
}

如果您想使用 __get - 您 必须 调用未定义的 属性。在这种情况下,它不是

SessionManager::close()  // call method `close()`

必须是:

$sm = new SessionManager;
$sm->propertyName;   // trying to access undefined property `propertyName` of an object

考虑到

Property overloading only works in object context.

这意味着尝试像

一样访问静态 属性
SessionManager::staticProperty;

不会__get一起工作。