单例模式
<?php //单例模式 class Request { private static $request; protected $method; protected $uriPath; public function __construct() { } private function __clone() { // TODO: Implement __clone() method. } public static function getRequest() { if (self::$request == null) { self::$request = new Request(); } self::$request->method = $_SERVER['REQUEST_METHOD']; self::$request->uriPath = $_SERVER['REQUEST_URI']; return self::$request; } public function getMehod() { return $this->method; } public function getUriPath() { return $this->uriPath; } public function isMethod($method): bool { return $this->method === strtoupper($method); } } $request = Request::getRequest(); var_dump($request->isMethod('post')); var_dump($request->getMehod()); var_dump($request->getUriPath());