3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php abstract class DB { private static $DBCONN = NULL; private static $OLog = NULL; private static function DBConnection() { if ( self::$DBCONN === NULL) { self::$DBCONN = new PDO('mysql:host=localhost;dbname=test', 'root', ''); } return self::$DBCONN; } public static function setLogger(Logger $logger) { self::$OLog = $logger; } public static function insertWithParams($SQL, $params){ try { self::$OLog->writeToFile( LogType::SQL, $SQL); self::$OLog->writeToFile( LogType::SQL_PARAMS, implode(",", $params)); $statement = self::DBConnection()->prepare($SQL); foreach ($params as $param => &$value) { $statement->bindParam($param, $value, PDO::PARAM_STR); echo 'Param = ' . $param . ' - ' . $value . '</br>'; } $retour = $statement->execute(); self::$OLog->writeToFile( LogType::SUCCESS); return $retour; } catch (Exception $e) { self::$OLog->writeToFile( LogType::ERROR . $e->getMessage()); return $e->getMessage(); } } } class Logger { private $fileToWrite = NULL; private function setFileToWrite(string $file) { if (file_exists($file)) { $this->fileToWrite = $file; return TRUE; } else { throw new Exception("Le fichier $file n existe pas."); } } public static function createLogger(string $fileToWrite) { $oLogger = new Logger(); try { if ($oLogger->setFileToWrite($fileToWrite)) { return $oLogger; } } catch (Exception $e) { //return $e->getMessage(); return NULL; } } public function writeToFile(string $LogType, string $message = '') : bool { //var_dump(debug_backtrace()); if (file_put_contents( $this->fileToWrite, date("Y-m-d h:i:sa") . ' - ' . 'From ' . debug_backtrace()[2]['class'] . ' - ' . $LogType . $message . PHP_EOL, FILE_APPEND) != FALSE ) { return TRUE; } else { return FALSE; } } } $oLogger = Logger::createLogger('log.txt'); DB::setLogger($oLogger);
based on pKr7m
Output for 7.3.0alpha1
Fatal error: Uncaught TypeError: Argument 1 passed to DB::setLogger() must be an instance of Logger, null given, called in /in/OKPCV on line 90 and defined in /in/OKPCV:15 Stack trace: #0 /in/OKPCV(90): DB::setLogger(NULL) #1 {main} thrown in /in/OKPCV on line 15
Process exited with code 255.
Output for 7.0.30 - 7.2.6
Warning: file_exists(): open_basedir restriction in effect. File(log.txt) is not within the allowed path(s): (/tmp:/in) in /in/OKPCV on line 48 Fatal error: Uncaught TypeError: Argument 1 passed to DB::setLogger() must be an instance of Logger, null given, called in /in/OKPCV on line 90 and defined in /in/OKPCV:15 Stack trace: #0 /in/OKPCV(90): DB::setLogger(NULL) #1 {main} thrown in /in/OKPCV on line 15
Process exited with code 255.
Output for hhvm-3.22.0
Fatal error: Uncaught TypeError: Argument 1 passed to DB::setLogger() must be an instance of Logger, null given in /in/OKPCV:-1 Stack trace: #0 /in/OKPCV(-1): DB::setLogger() #1 {main}
Process exited with code 255.
Output for hhvm-3.18.5 - 3.21.3
Fatal error: Uncaught TypeError: Argument 1 passed to DB::setLogger() must be an instance of Logger, null given in /in/OKPCV:17 Stack trace: #0 /in/OKPCV(90): DB::setLogger() #1 {main}
Process exited with code 255.
Output for 5.6.30
Fatal error: Default value for parameters with a class type hint can only be NULL in /in/OKPCV on line 70
Process exited with code 255.