Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

PHP design pattern: singleton pattern

Shulou Source: shulou.com Published: 2022-06-03 05:56:11 11月15日 Update

Application scenarios:

In a single http request, a database connection is used in multiple places, and with a singleton, you only need to create one connection.

A huge amount of memory space is wasted when a large class is instantiated multiple times. Using singleton mode allows multiple objects to share the static space of a class in memory.

Code example:

Verify:

$conn1=DBHelper::getInstance (); $conn2=DBHelper::getInstance (); echo $conn1= $conn2

Tags: Memory multiple space pattern huge code can make place scene instance object huge data database time example static application validation design pattern Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Apple vpn Shulou Tech Info Redmi