3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $config = [ 'fetchAs' => PDO::FETCH_CLASS, 'use' => 'mysql', 'drivers' => [ 'mysql' => [ 'host' => '127.0.0.1', # skip resolving localhost go directly to 127.0.0.1 ( Optional ) 'port' => 3306, # default mysql port ( Optional ) 'database' => 'twitbot', 'username' => 'root', 'password' => 'toor', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', # ( Optional ) 'persist' => true, # use persistent connection ( true | false ) 'prefix' => '', # ( Optional ) 'socket' => '', # unix socket example /tmp/mysql.sock, leave blank if you are using host ] ] ]; class Manager{ public $manager; public function __construct( array $config ) { $this->manager = new ConnectionManager( $config ); } public function getConnection() { return $this->manager->createConnection(); } } class ConnectionManager { private $config = []; private $availableDrivers = []; private $supportedDrivers = ['mysql']; public function __construct( array $config ) { $this->config = $config; $this->availableDrivers = PDO::getAvailableDrivers(); } public function createConnection() { if(empty( $this->config['use'] )) throw new Exception('Database driver must be set!', 1); # check if PDO Driver was compiled with PHP $driver = strtolower( $this->config['use'] ); if ( ! in_array( $driver, $this->availableDrivers, true )) throw new Exception('Missing PDO driver ' . $driver, 1); if ( ! in_array( $driver, $this->supportedDrivers, true )) throw new Exception($driver . ' is not supported!', 1); $reflection = new ReflectionClass(ucfirst($driver) . 'Connection'); return $reflection->newInstanceArgs([$this->config])->connect(); } } abstract class Connection { private $options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_CASE => PDO::CASE_NATURAL, PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, PDO::ATTR_STRINGIFY_FETCHES => false ]; public $config = []; public function __construct( array $config ) { $this->config = $config; $this->options[ PDO::ATTR_PERSISTENT ] = $this->getDriverKey('persist'); $this->options[ PDO::ATTR_DEFAULT_FETCH_MODE ] = $this->config['fetchAs']; } protected function establishConnection( $dsn = null ) { return new PDO( $dsn, $this->getDriverKey('username'), $this->getDriverKey('password'), $this->options ); } public function getDriverKey( $key = null ) { $value = $this->config['drivers'][ $this->config['use'] ][ $key ]; return ( isset($value) && ! empty($value) ) ? $value : false; } public function setOptions( array $options ) { $this->options = $options; } public function getOptions() { return $this->options; } } class MysqlConnection extends Connection{ private $dsn; public $connection; public function __construct( array $config ) { parent::__construct( $config ); $this->dsn = $this->makeDsn(); } private function makeDsn(){ return ( $this->isUsingSocket( $this->config ) ) ? $this->useSocket() : $this->useHost(); } private function isUsingSocket(){ if( empty($this->getDriverKey('socket')) && ! $this->getDriverKey('socket') ) return false; return ( is_readable($this->getDriverKey('socket')) ) ? true : false; # check if the socket file path is readable } private function isUsingPort() { return ( is_numeric($this->getDriverKey('port')) ) ? ';port=' . $this->getDriverKey('port') : ''; } private function useSocket() { return 'mysql:unix_socket=' . $this->getDriverKey('socket') . ';dbname=' . $this->getDriverKey('database') . ';charset=' . $this->getDriverKey('charset'); } private function useHost() { return 'mysql:host=' . $this->getDriverKey('host') . $this->isUsingPort() . ';dbname=' . $this->getDriverKey('database') . ';charset=' . $this->getDriverKey('charset'); } public function connect() { # check if collation is set if( $this->getDriverKey('collation') ){ #$this->options[ PDO::MYSQL_ATTR_INIT_COMMAND ] = "SET NAMES '" . $this->getDriverKey('charset') . "' COLLATE '" . $this->getDriverKey('collation') . "'"; } return $this->connection = $this->establishConnection( $this->dsn ); } } $manager = new Manager( $config ); var_dump($manager);
Output for 5.5.0 - 7.3.1
object(Manager)#1 (1) { ["manager"]=> object(ConnectionManager)#2 (3) { ["config":"ConnectionManager":private]=> array(3) { ["fetchAs"]=> int(8) ["use"]=> string(5) "mysql" ["drivers"]=> array(1) { ["mysql"]=> array(10) { ["host"]=> string(9) "127.0.0.1" ["port"]=> int(3306) ["database"]=> string(7) "twitbot" ["username"]=> string(4) "root" ["password"]=> string(4) "toor" ["charset"]=> string(4) "utf8" ["collation"]=> string(15) "utf8_unicode_ci" ["persist"]=> bool(true) ["prefix"]=> string(0) "" ["socket"]=> string(0) "" } } } ["availableDrivers":"ConnectionManager":private]=> array(1) { [0]=> string(6) "sqlite" } ["supportedDrivers":"ConnectionManager":private]=> array(1) { [0]=> string(5) "mysql" } } }
Output for 5.4.0 - 5.4.45
Fatal error: Can't use method return value in write context in /in/4uilc on line 153
Process exited with code 255.