3v4l.org

run code in 300+ PHP versions simultaneously
<?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 ); } }
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Fatal error: Uncaught PDOException: could not find driver in /in/o1Km6:6 Stack trace: #0 /in/o1Km6(6): PDO->__construct('mysql:......;ch...', 'username', Object(SensitiveParameterValue)) #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 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 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.28
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.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
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.1.6, 5.2.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.3.11, 4.4.0 - 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.

preferences:
304.9 ms | 401 KiB | 459 Q