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(); 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);

preferences:
58.48 ms | 402 KiB | 5 Q