I think you misunderstand Singleton. It means one instance, not an instance that stays loaded.
It is there anytime to call a method. In fact it's the opposite of remaining loaded.
Take a look at this a singonton db class. It does not stay loaded. Rather singonton means one occurance only, it prevents several instances loading at once. In a instance method, you could have several new calls to a class, not a singleton.
Bottom line, it's used as needed, not loaded over and over again. In fact never loaded, static classes always available.
<?php
namespace Helpers;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Container\Container;
use Illuminate\Events\Dispatcher;
use PDO;
class DbPdo
{
protected static $dbh = null;
protected function __construct()
{
}
protected function __clone()
{
}
public static function dbh()
{
if (self::$dbh === null) {
try {
// I've run into problem where
// SET NAMES "UTF8" not working on some hostings.
// Specifiying charset in DSN fixes the charset problem perfectly!
$opt = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];
$dsn = DB_TYPE . ':host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=utf8';
self::$dbh = new PDO($dsn, DB_USER, DB_PASS, $opt);
return self::$dbh;
} catch (PDOException $e) {
//in the event of an error record the error to ErrorLog.html
Logger::newMessage($e);
Logger::customErrorMsg();
}
} else {
return self::$dbh;
}
}
/**
* run raw sql queries
* @param string $sql sql command
* @return return query
*/
public static function raw($sql)
{
return self::dbh()->query($sql);
}
/**
* method for selecting records from a database
* @param string $sql sql query
* @param array $array named params
* @param object $fetchMode
* @param string $class class name
* @return array returns an array of records
*/
public static function select($sql, $array = array(), $fetchMode = PDO::FETCH_OBJ, $class = '')
{
$stmt = self::dbh()->prepare($sql);
foreach ($array as $key => $value) {
if (is_int($value)) {
$stmt->bindValue("$key", $value, PDO::PARAM_INT);
} else {
$stmt->bindValue("$key", $value);
}
}
$stmt->execute();
if ($fetchMode === PDO::FETCH_CLASS) {
return $stmt->fetchAll($fetchMode, $class);
} else {
return $stmt->fetchAll($fetchMode);
}
}
public static function selectOne($sql, $array = array(), $fetchMode = PDO::FETCH_OBJ, $class = '')
{
$stmt = self::dbh()->prepare($sql);
foreach ($array as $key => $value) {
if (is_int($value)) {
$stmt->bindValue("$key", $value, PDO::PARAM_INT);
} else {
$stmt->bindValue("$key", $value);
}
}
$stmt->execute();
if ($fetchMode === PDO::FETCH_CLASS) {
return $stmt->fetch($fetchMode, $class);
} else {
return $stmt->fetch($fetchMode);
}
}
/**
* insert method
* @param string $table table name
* @param array $data array of columns and values
*/
/// More methods
}
I wish I had the mental smarts to explain it better.
Please, please google singleton, and study this stuff.
Singleton, static calls, etc has nothing to do with a list of data staying loaded.
I have a status List table and wants to load all the status on run time
Put in a temp table, or hard code an array, or use csv file.