3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* pseudo mysqli object */ class Foo { public $error; public function __construct($host, $user, $passwd, $dbname) { $this->host = $host; $this->user = $user; $this->pass = $passwd; $this->$dbname = $dbname; } public function query($sql, $resultmode) { if ($sql !== 'Valid Query') { $this->error = 'Expected "Valid Query"'; } return new Bar; } } /** pseudo mysql_stmt object */ class Bar { public function fetch_assoc() { echo 'I was successful'; } } class Conn { private $conn; public function __construct(...$args) { $this->conn = new Foo(...$args); } public function query($query, $resultmode = 1) { $d = $this->conn->query($query, $resultmode); if (!empty($this->conn->error)) { throw new \RuntimeException($this->conn->error); } return $d; } } //Your example $con = 'my_var'; $$con = new Conn('host', 'user', 'pass', 'base'); //example usage of valid query $q = $my_var->query('Valid Query'); $q->fetch_assoc(); echo PHP_EOL. '-----' . PHP_EOL; //example use of invalid query throwing an exception before `fetch_assoc` $q = $my_var->query('This is not valid'); $q->fetch_assoc();
Output for 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
Deprecated: Creation of dynamic property Foo::$host is deprecated in /in/PlZEs on line 10 Deprecated: Creation of dynamic property Foo::$user is deprecated in /in/PlZEs on line 11 Deprecated: Creation of dynamic property Foo::$pass is deprecated in /in/PlZEs on line 12 Deprecated: Creation of dynamic property Foo::$base is deprecated in /in/PlZEs on line 13 I was successful ----- Fatal error: Uncaught RuntimeException: Expected "Valid Query" in /in/PlZEs:51 Stack trace: #0 /in/PlZEs(70): Conn->query('This is not val...') #1 {main} thrown in /in/PlZEs on line 51
Process exited with code 255.
Output for 7.0.33, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33
I was successful ----- Fatal error: Uncaught RuntimeException: Expected "Valid Query" in /in/PlZEs:51 Stack trace: #0 /in/PlZEs(70): Conn->query('This is not val...') #1 {main} thrown in /in/PlZEs on line 51
Process exited with code 255.
Output for 5.6.38
I was successful ----- Fatal error: Uncaught exception 'RuntimeException' with message 'Expected "Valid Query"' in /in/PlZEs:51 Stack trace: #0 /in/PlZEs(70): Conn->query('This is not val...') #1 {main} thrown in /in/PlZEs on line 51
Process exited with code 255.

preferences:
156.63 ms | 409 KiB | 5 Q