3v4l.org

run code in 300+ PHP versions simultaneously
<?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(); } } 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);
Output for 8.0.0 - 8.0.12, 8.0.14 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Warning: file_exists(): open_basedir restriction in effect. File(log.txt) is not within the allowed path(s): (/tmp:/in:/etc) in /in/pKr7m on line 48 Fatal error: Uncaught TypeError: DB::setLogger(): Argument #1 ($logger) must be of type Logger, string given, called in /in/pKr7m on line 89 and defined in /in/pKr7m:15 Stack trace: #0 /in/pKr7m(89): DB::setLogger('Le fichier log....') #1 {main} thrown in /in/pKr7m on line 15
Process exited with code 255.
Output for 8.0.13
Fatal error: Uncaught TypeError: DB::setLogger(): Argument #1 ($logger) must be of type Logger, string given, called in /in/pKr7m on line 89 and defined in /in/pKr7m:15 Stack trace: #0 /in/pKr7m(89): DB::setLogger('Le fichier log....') #1 {main} thrown in /in/pKr7m on line 15
Process exited with code 255.
Output for 7.3.32 - 7.3.33, 7.4.33
Fatal error: Uncaught TypeError: Argument 1 passed to DB::setLogger() must be an instance of Logger, string given, called in /in/pKr7m on line 89 and defined in /in/pKr7m:15 Stack trace: #0 /in/pKr7m(89): DB::setLogger('Le fichier log....') #1 {main} thrown in /in/pKr7m on line 15
Process exited with code 255.
Output for 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.32
Warning: file_exists(): open_basedir restriction in effect. File(log.txt) is not within the allowed path(s): (/tmp:/in:/etc) in /in/pKr7m on line 48 Fatal error: Uncaught TypeError: Argument 1 passed to DB::setLogger() must be an instance of Logger, string given, called in /in/pKr7m on line 89 and defined in /in/pKr7m:15 Stack trace: #0 /in/pKr7m(89): DB::setLogger('Le fichier log....') #1 {main} thrown in /in/pKr7m on line 15
Process exited with code 255.
Output for 7.1.0 - 7.1.20
Warning: file_exists(): open_basedir restriction in effect. File(log.txt) is not within the allowed path(s): (/tmp:/in) in /in/pKr7m on line 48 Fatal error: Uncaught TypeError: Argument 1 passed to DB::setLogger() must be an instance of Logger, string given, called in /in/pKr7m on line 89 and defined in /in/pKr7m:15 Stack trace: #0 /in/pKr7m(89): DB::setLogger('Le fichier log....') #1 {main} thrown in /in/pKr7m on line 15
Process exited with code 255.

preferences:
165.7 ms | 402 KiB | 168 Q