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();
- 1 回答
- 0 關注
- 149 瀏覽
添加回答
舉報