我觉得PHP静态字段仅在整个请求中是静态的。
我的控制器类中有这个代码:
static $a = 2;
public function debug()
{
var_dump(self::$a++);
var_dump(self::$a++);
}
无论我请求debug
多少次,它都会输出:
int 2
int 3
与我对java中的static
的了解非常不同。
答案 0 :(得分:1)
是的,PHP中的静态是真实的"静态的。
您观察到的是PHP和Java中不同应用程序生命周期的结果。
在Java中,Web应用程序在WebServer(HTTP服务器)内部运行,在初始类加载后,在以下请求中重用已经加载的内容。因此,类(和静态属性)初始化仅在应用程序生命周期中发生一次。
对于典型的PHP Web应用程序,它看起来有点不同。
HTTP服务器是独立的应用程序,它监听HTTP请求并按需运行PHP(并非所有HTTP请求都必须传递给PHP)。 PHP作为一个单独的进程运行,请求被传递,并且在收到应答后,进程被丢弃。每个请求都由完全独立的进程处理。每次都从头开始加载和初始化类(和静态属性)。
下面是用PHP编写的简单(非常)HTTP服务器,它将模拟Java WebServer的工作方式。
<?php
class Server {
private $socket;
private $routes = [];
public function __construct($address, $port, $backlog = 5) {
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
throw new Exception("socket_create() failed: reason: " . socket_strerror(socket_last_error($socket)));
}
if (socket_bind($socket, $address, $port) === false) {
throw new Exception("socket_bind() failed: reason: " . socket_strerror(socket_last_error($socket)));
}
if (socket_listen($socket, $backlog) === false) {
throw new Exception("socket_listen() failed: reason: " . socket_strerror(socket_last_error($socket)));
}
$this->socket = $socket;
}
public function listen() {
while( ($requestSocket = socket_accept($this->socket)) !== false ) {
$this->handleRequestSocket($requestSocket);
}
throw new Exception("socket_accept() failed: reason: " . socket_strerror(socket_last_error($this->socket)));
}
public function registerController($url, $controller) {
$this->routes[$url] = $controller;
}
private function handleRequestSocket($socket) {
$buffer = "";
while(false !== ($part = socket_read($socket, 1024, PHP_NORMAL_READ))){
$buffer .= $part;
if(substr($buffer, -4) == "\r\n\r\n") break;
}
$buffer = trim($buffer);
echo "\n======\n$buffer\n======\n";
$response = $this->handleRequest($buffer);
if (null === $response){
socket_write($socket, "HTTP/1.1 404 Not Found\r\nConnection: close\r\n\r\n");
} else {
socket_write($socket, "HTTP/1.1 200 OK\r\nContent-Length: ".strlen($response)."\r\n\r\n$response");
}
socket_close($socket);
}
private function handleRequest($raw) {
$lines = explode("\r\n", $raw);
$req = explode(" ", $lines[0]);
$method = $req[0];
$url = $req[1];
if(isset($this->routes[$url])) {
return (string) (is_callable($this->routes[$url]) ? $this->routes[$url]($raw) : $this->routes[$url]);
}
return null;
}
}
class ControllerWithStatic {
private static $static = 0;
public function handle() {
return "Hello from static: " . (self::$static++) . "\n";
}
}
$server = new Server($argv[1], $argv[2]);
$c = new ControllerWithStatic();
$server->registerController("/", "Hello world\n");
$server->registerController("/closure", function(){return "Hello world from closure\n";});
$server->registerController("/static", [$c, 'handle']);
$server->registerController("/static2", function(){
return (new ControllerWithStatic())->handle();
});
$server->listen();
使用
运行它php server.php HOST PORT
e.g。
php server.php 127.0.0.1 8080
现在在您的浏览器http://127.0.0.1:8080/static
或http://127.0.0.1:8080/static2
中打开,您将获得
Hello from static: 0
Hello from static: 1
Hello from static: 2
...
只要您不重新启动服务器,数字就会增加。