<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                [TOC] ## 示例 ### 概念示例 <details> <summary>main.php</summary> ``` <?php class Singleton { private static $instances = []; protected function __construct() { } protected function __clone() { } public function __wakeup() { throw new \Exception("Cannot unserialize a singleton."); } public static function getInstance(): Singleton { $cls = static::class; if (!isset(self::$instances[$cls])) { self::$instances[$cls] = new static(); } return self::$instances[$cls]; } public function someBusinessLogic() { // ... } } $s1 = Singleton::getInstance(); $s2 = Singleton::getInstance(); if ($s1 === $s2) { echo "相等"; } else { echo "不相等"; } ``` </details> <br /> 輸出 ``` 相等 ``` ### 單例日志與配置 采用單例服用的方式 <details> <summary>main.php</summary> ``` <?php class Singleton { // 使用數組,可以被其他所有單利類所繼承 private static $instances = []; protected function __construct() { } protected function __clone() { } public function __wakeup() { throw new \Exception("Cannot unserialize singleton"); } public static function getInstance() { $subclass = static::class; if (!isset(self::$instances[$subclass])) { self::$instances[$subclass] = new static(); } return self::$instances[$subclass]; } } class Logger extends Singleton { private $fileHandle; protected function __construct() { $this->fileHandle = fopen('php://stdout', 'w'); } public function writeLog(string $message): void { $date = date('Y-m-d'); fwrite($this->fileHandle, "$date: $message\n"); } public static function log(string $message): void { $logger = static::getInstance(); $logger->writeLog($message); } } class Config extends Singleton { // 屬性只有一份 private $hashmap = []; public function getValue(string $key): string { return $this->hashmap[$key]; } public function setValue(string $key, string $value): void { $this->hashmap[$key] = $value; } } Logger::log("Started!"); $l1 = Logger::getInstance(); $l2 = Logger::getInstance(); if ($l1 === $l2) { Logger::log("Logger 相同"); } else { Logger::log("Loggers 不同"); } $config1 = Config::getInstance(); $login = "test_login"; $password = "test_password"; $config1->setValue("login", $login); $config1->setValue("password", $password); $config2 = Config::getInstance(); if ($login == $config2->getValue("login") && $password == $config2->getValue("password")) { Logger::log("config 相等"); } Logger::log("Finished!"); ``` </details> <br /> 輸出 ``` 2020-09-24: Started! 2020-09-24: Logger 相同 2020-09-24: config 相等 2020-09-24: Finished! ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看