为什么我的desctruct方法被过早调用?

时间:2018-04-25 16:12:28

标签: php pdo

为了更好地理解,我简化了课程。为什么method2给出错误self :: $ dbconn为null?删除__desctruc()时它可以正常工作吗?

我这样称呼它:

$test = new TestCtrl();
$test->getList(123);

...

class TestCtrl {

        private static $dbconn;

        function __construct(){
            self::$dbconn = 'assume this is my db connection...';
        }

        function __destruct() {
            self::$dbconn = null;
        }

        private function method1($contact){
            $metas = self::method2();
            return $metas;
        }
        private static function method2(){

            $res = self::$dbconn;
            return $res;
        }

        public function getList($contact){
            return self::method1($contact);
        }

    }

1 个答案:

答案 0 :(得分:0)

因为您已将$ dbconn声明为静态,所以它将在调用之间设置为其原始值。将其更改为非静态变量,它应该可以正常工作。