为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
杨魅力 2023-07-08 16:17:22
我在通过包含的文件在类之间调用静态变量/方法时遇到问题。这是代码示例:<?phpclass 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。我无法弄清楚问题是什么,并尝试查看包含脚本是否会导致此类问题,但我什至不知道应该寻找什么。请帮忙,谢谢。
查看完整描述

1 回答

?
呼啦一阵风

TA贡献1802条经验 获得超6个赞

经过一些测试发现 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 方法中读取同一类的静态实例。


这里的脚本应该是这样的,因为我的解释不清楚:


<?php


class Router

{

    static $instance;


    public static function GetInstance()

    {

        if(self::$instance == null)

            self::$instance = new self;


        return self::$instance;

    }


    function __construct()

    {


    }


    public function LoadFiles(){

        

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

$router->LoadFiles();


查看完整回答
反对 回复 2023-07-08
  • 1 回答
  • 0 关注
  • 96 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信