3v4l.org

run code in 300+ PHP versions simultaneously
<?php //============================================== // 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 git.master, git.master_jit, rfc.property-hooks
Warning: Private methods cannot be final as they are never overridden by other classes in /in/uB2Dj on line 128 Warning: Private methods cannot be final as they are never overridden by other classes in /in/uB2Dj on line 147 Fatal error: Uncaught Error: Class "FileIOException" not found in /in/uB2Dj:151 Stack trace: #0 /in/uB2Dj(89): PDOWrapper->loadConfigFile('~/.redshift/con...') #1 /in/uB2Dj(26): PDOWrapper->reset() #2 /in/uB2Dj(188): PDOWrapper->__construct() #3 {main} thrown in /in/uB2Dj on line 151
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:
48.62 ms | 401 KiB | 8 Q