使應用中只存在一個物件的實體,并且使這個單實體負責所有對該物件的呼叫
final class Singleton{ private static ?Singleton $instance = null; public static function getInstance(): Singleton { if (static::$instance === null) { static::$instance = new static(); } return static::$instance; } private function __construct(){ } private function __clone(){ } private function __wakeup(){ } }
型別前面的問號表示引數或回傳值可為空(null),是PHP7的新特性
例如,?string $str 表示$str的值可以為null或字串
此用法不只局限于靜態型別,類和介面也可使用,例如,?MyInterface
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/172123.html
標籤:PHP
