简体   繁体   English

Php 包含的脚本不读取相同的 static 变量?

[英]Php Included scripts don't read same static variables?

i am having a problem with calling static variables/methods between classes through an included file.我在通过包含的文件在类之间调用 static 变量/方法时遇到问题。

that's code example:这是代码示例:

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

where the script Routes::doSomething();其中脚本Routes::doSomething(); in test2.php will not be able to read the static $instance in Router class.在 test2.php 将无法读取路由器 class 中的 static $ 实例。

i can't figure out what's the problem and tried to look if including scripts will cause such issue but i don't even know what i should look for.我不知道是什么问题,并试图查看是否包含脚本会导致此类问题,但我什至不知道我应该寻找什么。

help please, thanks.请帮忙,谢谢。

After some tests turn out PHP cannot handle such script.经过一些测试,PHP 无法处理此类脚本。 Because of the process of declaring variables and how PHP handle it.因为声明变量的过程以及 PHP 如何处理它。

There's no source that i can give to support what i am about to say, its only what i understood so far from my tests.我无法提供任何资料来支持我要说的话,这只是我迄今为止从我的测试中所理解的。 But i will try to explain it with my low English level hoping that might help someone sometime later.但我会尝试用我的低英语水平来解释它,希望以后的某个时候可能会对某人有所帮助。

So in PHP when you try to declare a static/dynamic variable with class object like this:因此,当您尝试使用 class object 像这样声明静态/动态变量时,在 PHP 中:

self::$Instance = new TestClass;

Php will process the command in steps to work in this way: Php 将分步处理命令,以这种方式工作:

Step#1: Declare self::$Instance as null步骤#1: Declare self::$Instance as null

Step#2: Create temporary_object from TestClass class步骤#2: Create temporary_object from TestClass class

step#3: Run __construct method in temporary_object then wait until its done step#3: Run __construct method in temporary_object then wait until its done

Step#4: Set self::$Instance as temporary_object which is instanceof TestClass步骤#4: Set self::$Instance as temporary_object which is instanceof TestClass

The problem in my code as you can see that i was trying to include .php file inside of __construct method.如您所见,我的代码中的问题是我试图在__construct方法中包含.php 文件。 The included file will run a static function that will try to read self::$instance in Router class.包含的文件将运行 static function将尝试读取路由器 class 中的 self::$instance While static::$Instance is not set and still equal null because __construct isn't done yet.虽然static::$Instance未设置但仍等于null因为__construct尚未完成。

So basically don't try to read static instance of the same class inside __construct method.所以基本上不要尝试在 __construct 方法中读取相同 class 的 static 实例。

And here how the script should be, in cause my explanation was not clear:这里的脚本应该是怎样的,因为我的解释不清楚:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM