3v4l.org

run code in 300+ PHP versions simultaneously
<?php try { $db = DatabasePDOTest::getInstance(); var_dump($db->connectToDatabase('test')); } catch (Exception $ex) { var_dump($ex); } echo "end"; final class DatabasePDOTest extends PDO { const STANDARD_DB = 'standard'; const DBHOST = 'host'; const DBUSER = 'user'; const DBPASS = 'password'; const DBNAME = 'dbname'; private static $_instance = null; private $_persistent = false; private $_databases = array(); private $_databaseHost = null; private $_databaseUser = null; private $_databasePass = null; private $_database = null; private $_charset = 'utf8'; public function __construct($persistent = false, $pathAddition = null, $databaseIdentifier = null) { $this->_loadConfig($pathAddition); $this->_persistent = $persistent; if (! is_null($databaseIdentifier)) { $this->connectToDatabase($databaseIdentifier); } else { if (array_key_exists(self::STANDARD_DB, $this->_databases)) { $this->connectToDatabase($this->_databases[self::STANDARD_DB]); } else { throw new InvalidArgumentException('no database set and no standard found'); } } } public function connectToDatabase($name) { if (! array_key_exists($name, $this->_databases)) { throw new UnexpectedValueException('identifier "' . $name . '" for database does not exist!'); } $this->_databaseHost = $this->_databases[$name][self::DBHOST]; $this->_databaseUser = $this->_databases[$name][self::DBUSER]; $this->_databasePass = $this->_databases[$name][self::DBPASS]; $this->_database = $this->_databases[$name][self::DBNAME]; return $this->connect(); } public static function getInstance($persistent = true, $pathAddition = null, $databaseIdentifier = null) { if (self::$_instance === null) { self::$_instance = new DatabasePDOTest($persistent, $pathAddition, $databaseIdentifier); } return self::$_instance; } public function getOneResultFlat(PDOStatement $stmt) { $results = $stmt->fetchAll(PDO::FETCH_ASSOC); $return = array(); array_walk_recursive($results, function ($a) use (&$return) { $return[] = $a; }); return $return; } public function getMultipleResultFlat(PDOStatement $stmt) { $results = $stmt->fetchAll(PDO::FETCH_ASSOC); $returnArray = array(); foreach ($results as $value) { foreach ($value as $key => $var) { $returnArray[$key][] = $var; } } return $returnArray; } public function getResultArray($sql) { $stmt = $this->query($sql); return $stmt->fetchAll(PDO::FETCH_ASSOC); } private function connect() { parent::__construct(sprintf('mysql:host=%1$s;dbname=%2$s;charset=%3$s', $this->_databaseHost, $this->_database, $this->_charset), $this->_databaseUser, $this->_databasePass, array( PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_PERSISTENT => $this->_persistent )); return true; } private function _loadConfig($pathAddition = null) { $this->_databases = array( "test" => array( "host" => "localhost", "user" => "test", "password" => "test", "dbname" => "test" ), "standard" => "test" ); } }
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
object(PDOException)#2 (8) { ["message":protected]=> string(21) "could not find driver" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/qPgc4" ["line":protected]=> int(109) ["trace":"Exception":private]=> array(5) { [0]=> array(6) { ["file"]=> string(9) "/in/qPgc4" ["line"]=> int(109) ["function"]=> string(11) "__construct" ["class"]=> string(3) "PDO" ["type"]=> string(2) "->" ["args"]=> array(4) { [0]=> string(45) "mysql:host=localhost;dbname=test;charset=utf8" [1]=> string(4) "test" [2]=> object(SensitiveParameterValue)#3 (0) { } [3]=> array(3) { [20]=> bool(false) [3]=> int(2) [12]=> bool(true) } } } [1]=> array(6) { ["file"]=> string(9) "/in/qPgc4" ["line"]=> int(66) ["function"]=> string(7) "connect" ["class"]=> string(15) "DatabasePDOTest" ["type"]=> string(2) "->" ["args"]=> array(0) { } } [2]=> array(6) { ["file"]=> string(9) "/in/qPgc4" ["line"]=> int(48) ["function"]=> string(17) "connectToDatabase" ["class"]=> string(15) "DatabasePDOTest" ["type"]=> string(2) "->" ["args"]=> array(1) { [0]=> string(4) "test" } } [3]=> array(6) { ["file"]=> string(9) "/in/qPgc4" ["line"]=> int(72) ["function"]=> string(11) "__construct" ["class"]=> string(15) "DatabasePDOTest" ["type"]=> string(2) "->" ["args"]=> array(3) { [0]=> bool(true) [1]=> NULL [2]=> NULL } } [4]=> array(6) { ["file"]=> string(9) "/in/qPgc4" ["line"]=> int(3) ["function"]=> string(11) "getInstance" ["class"]=> string(15) "DatabasePDOTest" ["type"]=> string(2) "::" ["args"]=> array(0) { } } } ["previous":"Exception":private]=> NULL ["errorInfo"]=> NULL } end
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 object(PDOException)#2 (8) { ["message":protected]=> string(21) "could not find driver" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/qPgc4" ["line":protected]=> int(109) ["trace":"Exception":private]=> array(5) { [0]=> array(6) { ["file"]=> string(9) "/in/qPgc4" ["line"]=> int(109) ["function"]=> string(11) "__construct" ["class"]=> string(3) "PDO" ["type"]=> string(2) "->" ["args"]=> array(4) { [0]=> string(45) "mysql:host=localhost;dbname=test;charset=utf8" [1]=> string(4) "test" [2]=> object(SensitiveParameterValue)#3 (0) { } [3]=> array(3) { [20]=> bool(false) [3]=> int(2) [12]=> bool(true) } } } [1]=> array(6) { ["file"]=> string(9) "/in/qPgc4" ["line"]=> int(66) ["function"]=> string(7) "connect" ["class"]=> string(15) "DatabasePDOTest" ["type"]=> string(2) "->" ["args"]=> array(0) { } } [2]=> array(6) { ["file"]=> string(9) "/in/qPgc4" ["line"]=> int(48) ["function"]=> string(17) "connectToDatabase" ["class"]=> string(15) "DatabasePDOTest" ["type"]=> string(2) "->" ["args"]=> array(1) { [0]=> string(4) "test" } } [3]=> array(6) { ["file"]=> string(9) "/in/qPgc4" ["line"]=> int(72) ["function"]=> string(11) "__construct" ["class"]=> string(15) "DatabasePDOTest" ["type"]=> string(2) "->" ["args"]=> array(3) { [0]=> bool(true) [1]=> NULL [2]=> NULL } } [4]=> array(6) { ["file"]=> string(9) "/in/qPgc4" ["line"]=> int(3) ["function"]=> string(11) "getInstance" ["class"]=> string(15) "DatabasePDOTest" ["type"]=> string(2) "::" ["args"]=> array(0) { } } } ["previous":"Exception":private]=> NULL ["errorInfo"]=> NULL } end
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.6 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28
object(PDOException)#2 (8) { ["message":protected]=> string(21) "could not find driver" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/qPgc4" ["line":protected]=> int(112) ["trace":"Exception":private]=> array(5) { [0]=> array(6) { ["file"]=> string(9) "/in/qPgc4" ["line"]=> int(112) ["function"]=> string(11) "__construct" ["class"]=> string(3) "PDO" ["type"]=> string(2) "->" ["args"]=> array(4) { [0]=> string(45) "mysql:host=localhost;dbname=test;charset=utf8" [1]=> string(4) "test" [2]=> string(4) "test" [3]=> array(3) { [20]=> bool(false) [3]=> int(2) [12]=> bool(true) } } } [1]=> array(6) { ["file"]=> string(9) "/in/qPgc4" ["line"]=> int(66) ["function"]=> string(7) "connect" ["class"]=> string(15) "DatabasePDOTest" ["type"]=> string(2) "->" ["args"]=> array(0) { } } [2]=> array(6) { ["file"]=> string(9) "/in/qPgc4" ["line"]=> int(48) ["function"]=> string(17) "connectToDatabase" ["class"]=> string(15) "DatabasePDOTest" ["type"]=> string(2) "->" ["args"]=> array(1) { [0]=> string(4) "test" } } [3]=> array(6) { ["file"]=> string(9) "/in/qPgc4" ["line"]=> int(72) ["function"]=> string(11) "__construct" ["class"]=> string(15) "DatabasePDOTest" ["type"]=> string(2) "->" ["args"]=> array(3) { [0]=> bool(true) [1]=> NULL [2]=> NULL } } [4]=> array(6) { ["file"]=> string(9) "/in/qPgc4" ["line"]=> int(3) ["function"]=> string(11) "getInstance" ["class"]=> string(15) "DatabasePDOTest" ["type"]=> string(2) "::" ["args"]=> array(0) { } } } ["previous":"Exception":private]=> NULL ["errorInfo"]=> NULL } end
Output for 5.5.0 - 5.5.35, 5.6.0 - 5.6.28
object(PDOException)#2 (8) { ["message":protected]=> string(21) "could not find driver" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/qPgc4" ["line":protected]=> int(113) ["trace":"Exception":private]=> array(5) { [0]=> array(6) { ["file"]=> string(9) "/in/qPgc4" ["line"]=> int(113) ["function"]=> string(11) "__construct" ["class"]=> string(3) "PDO" ["type"]=> string(2) "->" ["args"]=> array(4) { [0]=> string(45) "mysql:host=localhost;dbname=test;charset=utf8" [1]=> string(4) "test" [2]=> string(4) "test" [3]=> array(3) { [20]=> bool(false) [3]=> int(2) [12]=> bool(true) } } } [1]=> array(6) { ["file"]=> string(9) "/in/qPgc4" ["line"]=> int(66) ["function"]=> string(7) "connect" ["class"]=> string(15) "DatabasePDOTest" ["type"]=> string(2) "->" ["args"]=> array(0) { } } [2]=> array(6) { ["file"]=> string(9) "/in/qPgc4" ["line"]=> int(48) ["function"]=> string(17) "connectToDatabase" ["class"]=> string(15) "DatabasePDOTest" ["type"]=> string(2) "->" ["args"]=> array(1) { [0]=> string(4) "test" } } [3]=> array(6) { ["file"]=> string(9) "/in/qPgc4" ["line"]=> int(72) ["function"]=> string(11) "__construct" ["class"]=> string(15) "DatabasePDOTest" ["type"]=> string(2) "->" ["args"]=> array(3) { [0]=> bool(true) [1]=> NULL [2]=> NULL } } [4]=> array(6) { ["file"]=> string(9) "/in/qPgc4" ["line"]=> int(3) ["function"]=> string(11) "getInstance" ["class"]=> string(15) "DatabasePDOTest" ["type"]=> string(2) "::" ["args"]=> array(0) { } } } ["previous":"Exception":private]=> NULL ["errorInfo"]=> NULL } end

preferences:
181.65 ms | 409 KiB | 243 Q