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(); echo $a->hasError();
Output for git.master, git.master_jit, rfc.property-hooks
Warning: Private methods cannot be final as they are never overridden by other classes in /in/R4IAo on line 158 Warning: Private methods cannot be final as they are never overridden by other classes in /in/R4IAo on line 177 Fatal error: Uncaught Error: Call to undefined method RedshiftDB::hasError() in /in/R4IAo:220 Stack trace: #0 {main} thrown in /in/R4IAo on line 220
Process exited with code 255.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
63.66 ms | 401 KiB | 8 Q