class Configurador { private static $instance; private $config; private function __construct() { $this->config = array( ' database' => array( 'host' => 'localhost', 'username' => 'my_username', 'password' => 'my_password', 'database' => 'my_database' ), 'email' => array( 'host' => 'smtp.gmail.com', 'username' => 'my_email', 'password' => 'my_email_password' ) ); } public static function getInstance() { if (!self::$instance) { self::$instance = new Configurador(); } return self::$instance; } public function getConfig($key) { return $this->config[$key]; } }
$config = Configurador::getInstance(); $database = $config->getConfig('database'); $email = $config->getConfig('email'); echo $database['host']; // outputs: localhost echo $email['username']; // outputs: my_emailFrom the code example above, the `Configurador` singleton is included in a package library which can be used to implement configuration classes or any classes that require only one instance to be created in a web application.