- Руководство по PHP
- Приступая к работе
- Установка и настройка
- Справочник языка
- Безопасность
- Отличительные особенности
- Справочник функций
- Variable and Type Related Extensions
- Text Processing
- Affecting PHP's Behaviour
- Session Extensions
- Other Basic Extensions
- Database Extensions
- XML Manipulation
- Web Services
- Credit Card Processing
- Mathematical Extensions
- Cryptography Extensions
- Human Language and Character Encoding Support
- File System Related Extensions
- Process Control Extensions
- Mail Related Extensions
- Authentication Services
- Other Services
- Compression Extensions
- Calendar and Event Related Extensions
- Non-Text MIME Output
- Image Processing and Generation
- Audio Formats Manipulation
- Command Line Specific Extensions
- Windows Only Extensions
- Server Specific Extensions
- PHP at the Core: A Hacker's Guide to the Zend Engine
- Preface
- The "counter" Extension - A Continuing Example
- The PHP 5 build system
- Extension structure
- Memory management
- Working with variables
- Writing functions
- Working with classes and objects
- Working with resources
- Working with INI settings
- Working with streams
- PDO Driver How-To
- Extension FAQs
- Zend Engine 2 API reference
- Zend Engine 1
- The future: PHP 6 and Zend Engine 3
- ЧАВО — ЧАВО: ЧАсто задаваемые Вопросы и Ответы на них
- Appendices
- История PHP и смежных проектов
- Migrating from PHP 5.1.x to PHP 5.2.x
- Migrating from PHP 5.0.x to PHP 5.1.x
- Миграция с PHP 4 на PHP 5
- Отладка PHP
- Configure options
- Директивы php.ini
- List of Supported Timezones
- Extension Categorization
- Список псевдонимов функций
- List of Reserved Words
- List of Resource Types
- List of Supported Protocols/Wrappers
- Список доступных фильтров
- Список поддерживаемых транспортных протоколов
- Таблица сравнения типов в PHP
- List of Parser Tokens
- Userland Naming Guide
- Об этом руководстве
- Open Publication License
- Список функций
- Что отсутствует в этом руководстве
Коментарии
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;
}
}