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 git.master, git.master_jit, rfc.property-hooks
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.

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:
29.38 ms | 406 KiB | 5 Q