3v4l.org

run code in 300+ PHP versions simultaneously
<?php class InvalidFileFormatException extends Exception { // Redefine the exception so message isn't optional public function __construct($message, $code = 0, Exception $previous = null) { parent::__construct($message, $code, $previous); } // custom string representation of object public function __toString() { if ($code > 0) { return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; } else { return __CLASS__ . ": {$this->message}\n"; } } } class FileIOException extends Exception { // Redefine the exception so message isn't optional public function __construct($message, $code = 0, Exception $previous = null) { parent::__construct($message, $code, $previous); } // custom string representation of object public function __toString() { return __CLASS__ . ": {$this->message}\n"; } } //============================================== // class //============================================== abstract class PDOWrapper { //============================================== // variables //============================================== private $exception = Null; private $username = Null; private $password = Null; private $dsn = Null; private $client = Null; private $pdoStatement = Null; //============================================== // constructor //============================================== final public function __construct() { $this->reset(); } //============================================== // overrides //============================================== abstract protected function getConfigFile(); //============================================== // main //============================================== final public function prepare($query) { if ($this->ready()) { $this->pdoStatement = $this->client->prepare($query); } return $this->checkError(); } final public function execute() { if ($this->ready() && $this->pdoStatement) { return $this->pdoStatement->execute(); } return $this->checkError(); } final public function fetch() { if ($this->ready() && $this->pdoStatement) { return $this->pdoStatement->fetch(); } return False; } final public function fetchAll() { if ($this->ready() && $this->pdoStatement) { return $this->pdoStatement->fetchAll(); } return False; } final public function query($query) { if ($this->ready()) { $this->pdoStatement = $this->client->query($query); } return $this->checkError(); } final public function ready() { return !$this->hasException(); } final public function reset() { $exception = Null; try { $this->loadConfigFile($this->getConfigFile()); //may throw error (see __get()) $this->client = new PDO($this->dsn, $this->username, $this->password); } catch (Exception $e) { $this->exception = $e; } } //============================================== // accessing //============================================== final public function hasException() { return isset($this->exception); } final public function getException() { return $this->exception; } final public function getRowCount() { if ($this->pdoStatement && !empty($this->pdoStatement->errorInfo()[2])) { return $this->pdoStatement->rowCount(); } return False; } final protected function setException($e) { $this->exception = $e; } //============================================== // utility //============================================== final private function checkError() { //pdoStatement->errorInfo() always returns array if ($this->pdoStatement == False) { $this->exception = new PDOException('Connection Error - Please Check Config File and Network'); return False; } if (!empty($this->pdoStatement->errorInfo()[2])) { $msg = $this->client->errorInfo()[2]; $errCode = $this->client->errorCode(); $this->exception = new PDOException($msg, $code = $errCode); return False; } return True; } final private function loadConfigFile($filepath) { $string = @file_get_contents($filepath); if ($string === False) { throw new FileIOException($filepath); } $json = @json_decode($string, true); if ($json === False || !$json['USERNAME'] || !$json['PASSWORD'] || !$json['DSN']) { throw new InvalidFileFormatException($filepath . ' : ' . json_last_error_msg(), $code = json_last_error()); } $this->username = $json['USERNAME']; $this->password = $json['PASSWORD']; $this->dsn = $json['DSN']; } } class MySqlDB extends PDOWrapper { //============================================== // overrides //============================================== final protected function getConfigFile() { return '~/.mysql/config.json'; } } class RedshiftDB extends PDOWrapper { //============================================== // overrides //============================================== final protected function getConfigFile() { return '~/.redshift/config.json'; } } $a = new RedshiftDB();
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Warning: Private methods cannot be final as they are never overridden by other classes in /in/5oq21 on line 158 Warning: Private methods cannot be final as they are never overridden by other classes in /in/5oq21 on line 177
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33

preferences:
142.26 ms | 403 KiB | 225 Q