猿问

PHP 包含的脚本不读取相同的静态变量?

我在通过包含的文件在类之间调用静态变量/方法时遇到问题。


这是代码示例:


<?php


class Router

{

    static $instance;


    public static function GetInstance()

    {

        if(self::$instance == null)

            self::$instance = new self;


        return self::$instance;

    }


    function __construct()

    {


        include 'test2.php';


        /*  test2.php CONTENTS          */

        /*  Routes::doSomething();      */

    }



    public function doSomthing()

    {

        echo 1;

    }

}


class Routes

{

    // test.php script will call this function

    // this function will try to get Router static instance to call a dynamic function

    // keep in mind that the static instance of Router was already created

    // some how test2.php script will not be able to read the static instance and will create another one

    // and that will cause the page keep including and running the same script over and over and idk why

    public static function doSomething()

    {

        Router::GetInstance()->doSomthing();

    }


}


$router = Router::GetInstance();

其中 test2.php 中的脚本Routes::doSomething();将无法读取 Router 类中的静态 $instance。


我无法弄清楚问题是什么,并尝试查看包含脚本是否会导致此类问题,但我什至不知道应该寻找什么。


请帮忙,谢谢。


杨魅力
浏览 125回答 1
1回答

呼啦一阵风

经过一些测试发现 PHP 无法处理此类脚本。因为声明变量的过程以及PHP如何处理它。我无法提供任何来源来支持我要说的内容,这只是我迄今为止从测试中了解到的内容。但我会尝试用我较低的英语水平来解释它,希望以后能对某人有所帮助。因此,在 PHP 中,当您尝试使用类对象声明静态/动态变量时,如下所示:self::$Instance = new TestClass;PHP 将按步骤处理命令,以这种方式工作:步骤1 :Declare self::$Instance as null第2步 :Create temporary_object from TestClass class步骤#3:Run __construct method in temporary_object then wait until its done步骤4 :Set self::$Instance as temporary_object which is instanceof TestClass我的代码中存在问题,您可以看到我试图在__construct方法中包含.php 文件。包含的文件将运行一个静态函数,该函数将尝试读取Router类中的 self::$instance 。虽然static::$Instance未设置并且仍然等于null,因为__construct尚未完成。所以基本上不要尝试在 __construct 方法中读取同一类的静态实例。这里的脚本应该是这样的,因为我的解释不清楚:<?phpclass Router{&nbsp; &nbsp; static $instance;&nbsp; &nbsp; public static function GetInstance()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if(self::$instance == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self::$instance = new self;&nbsp; &nbsp; &nbsp; &nbsp; return self::$instance;&nbsp; &nbsp; }&nbsp; &nbsp; function __construct()&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp; &nbsp; public function LoadFiles(){&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; include 'test2.php';&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; test2.php CONTENTS&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; Routes::doSomething();&nbsp; &nbsp; &nbsp; */&nbsp; &nbsp; }&nbsp; &nbsp; public function doSomthing()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; echo 1;&nbsp; &nbsp; }}class Routes{&nbsp; &nbsp; // test.php script will call this function&nbsp; &nbsp; // this function will try to get Router static instance to call a dynamic function&nbsp; &nbsp; // keep in mind that the static instance of Router was already created&nbsp; &nbsp; // some how test2.php script will not be able to read the static instance and will create another one&nbsp; &nbsp; // and that will cause the page keep including and running the same script over and over and idk why&nbsp; &nbsp; public static function doSomething()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Router::GetInstance()->doSomthing();&nbsp; &nbsp; }}$router = Router::GetInstance();$router->LoadFiles();
随时随地看视频慕课网APP
我要回答