note 129610 added to index
| From: | simulationlaboratory2024@gmail.com | Date: | Mon, 24 Jun 2024 03:32:23 +0000 |
| Subject: | note 129610 added to index | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221393@lists.php.net to get a copy of this message | ||
A better way to implement singleton desygn pattern and resquest data from a json file to connect to
a database mysql. this is from my development project.
<?php
namespace Simulab\Simulab\models\connections;
/**
* Desygn Pattern: Construction: Singleton method.
*/
class SimulabConnection
{
private static $instance = null;
private static $pdo = null;
private function __construct()
{
}
/**
* This method get information like servername, database, user and password from a json file
* All The informations about the database connections.
*/
public static function getJsonData():object{
$filename = '../../config/json/sgbd_informations.json';
if (file_exists($filename)) {
$data = file_get_contents($filename);
$infodb= json_decode($data);
return $infodb;
}else{
return null;
}
}
public static function connect():?\PDO{
if (is_null(self::$instance)) {
self::$instance = new self;
$infodb= self::getJsonData()!=null?self::getJsonData():null;
$server= $infodb->server;
$database= $infodb->database;
$user= $infodb->user;
$password= $infodb->password;
$options= [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION];
self::$pdo = new \PDO("mysql:host=$server;dbname=$database", $user, $password,
$options);
}
return self::$pdo;
}
}
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 190.115.174.151)
----
Manual Page -- https://php.net/manual/en/index.php
Edit -- https://main.php.net/note/edit/129610
Del: integrated -- https://main.php.net/note/delete/129610/integrated
Del: useless -- https://main.php.net/note/delete/129610/useless
Del: bad code -- https://main.php.net/note/delete/129610/bad+code
Del: spam -- https://main.php.net/note/delete/129610/spam
Del: non-english -- https://main.php.net/note/delete/129610/non-english
Del: in docs -- https://main.php.net/note/delete/129610/in+docs
Del: other reasons-- https://main.php.net/note/delete/129610
Reject -- https://main.php.net/note/reject/129610
Search -- https://main.php.net/manage/user-notes.php