我在通过包含的文件在类之间调用静态变量/方法时遇到问题。
这是代码示例:
<?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。
我无法弄清楚问题是什么,并尝试查看包含脚本是否会导致此类问题,但我什至不知道应该寻找什么。
请帮忙,谢谢。
呼啦一阵风