读过静态类和单例模式之间的区别?,没有一个答案列出了在单例上使用静态方法的任何优点,这让我想知道为什么有人会想要使用静态方法.
我在我的应用程序中使用ArrayList.
我想知道从Singleton类初始化ArrayList的确切过程.
该数据将用于其他一些活动.
有人可以帮助了解Singleton类吗?
<?php
//db connection class using singleton pattern
class dbConn {
//variable to hold connection object.
protected static $db;
//private construct – class cannot be instatiated externally.
private function __construct()
{
try { // assign PDO object to db variable
self::$db = new PDO('mysql:host=localhost;dbname=cricket', 'root', '');
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) { //Output error – would normally log this to error file rather than output to user.
echo "Connection Error: " . $e->getMessage();
}
}
// get connection function. Static …Run Code Online (Sandbox Code Playgroud)