Slim Route 似乎重置了静态变量
Slim Route seems to reset static variables
我使用 Slim 路由调用 RabbitBroker::setup()
并跟踪其响应。
设置方法将始终继续执行初始化过程。它从不 returns "already setup" 消息。我尝试使用 RabbitBroker::$isSetup
而不是 self::$isSetup
..一直忘记值..我在这里要疯了吗?
class RabbitBroker
{
private static $isSetup = false;
public static function setup() {
if (self::$isSetup) return "Connection was set already setup";
self::$isSetup = true;
// do some init...
return "Connection is now set by init"
}
}
您可能试图在请求之间共享静态变量的值。
PHP 是无状态的(像 HTTP),因为如果这样,每个脚本执行都有自己的静态变量。
所以设置一个静态变量只为当前请求设置它,而不是为后续请求设置它。
我使用 Slim 路由调用 RabbitBroker::setup()
并跟踪其响应。
设置方法将始终继续执行初始化过程。它从不 returns "already setup" 消息。我尝试使用 RabbitBroker::$isSetup
而不是 self::$isSetup
..一直忘记值..我在这里要疯了吗?
class RabbitBroker
{
private static $isSetup = false;
public static function setup() {
if (self::$isSetup) return "Connection was set already setup";
self::$isSetup = true;
// do some init...
return "Connection is now set by init"
}
}
您可能试图在请求之间共享静态变量的值。 PHP 是无状态的(像 HTTP),因为如果这样,每个脚本执行都有自己的静态变量。
所以设置一个静态变量只为当前请求设置它,而不是为后续请求设置它。