3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php // ----------------------------------- script1.php -------------- $provider = function() { $instance = new PDO('mysql:......;charset=utf8', 'username', 'password'); $instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $instance->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); return $instance; }; $factory = new StructureFactory( $provider ); $something = $factory->create('Something'); $foobar = $factory->create('Foobar'); // ----------------------------------- script2.php -------------- class StructureFactory { protected $provider = null; protected $connection = null; public function __construct( callable $provider ) { $this->provider = $provider; } public function create( $name) { if ( $this->connection === null ) { $this->connection = call_user_func( $this->provider ); } return new $name( $this->connection ); } }
based on 9X7Nm
Output for 7.0.0 - 7.3.0rc3
Fatal error: Uncaught PDOException: could not find driver in /in/o1Km6:6 Stack trace: #0 /in/o1Km6(6): PDO->__construct('mysql:......;ch...', 'username', 'password') #1 /in/o1Km6(38): {closure}() #2 /in/o1Km6(16): StructureFactory->create('Something') #3 {main} thrown in /in/o1Km6 on line 6
Process exited with code 255.
Output for 5.4.0 - 5.6.28
Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in /in/o1Km6:6 Stack trace: #0 /in/o1Km6(6): PDO->__construct('mysql:......;ch...', 'username', 'password') #1 [internal function]: {closure}() #2 /in/o1Km6(38): call_user_func(Object(Closure)) #3 /in/o1Km6(16): StructureFactory->create('Something') #4 {main} thrown in /in/o1Km6 on line 6
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Catchable fatal error: Argument 1 passed to StructureFactory::__construct() must be an instance of callable, instance of Closure given, called in /in/o1Km6 on line 12 and defined in /in/o1Km6 on line 29
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.2.17
Parse error: syntax error, unexpected T_FUNCTION in /in/o1Km6 on line 4
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_FUNCTION in /in/o1Km6 on line 4
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/o1Km6 on line 4
Process exited with code 255.