亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 149 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號