3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Reduced core class from MediaWiki to demonstrate an issue starting with 7.1.0alpha2 // https://github.com/wikimedia/mediawiki/blob/master/includes/Hooks.php class Hooks { protected static $handlers = array(); public static function register( $name, $callback ) { if ( !isset( self::$handlers[$name] ) ) { self::$handlers[$name] = array(); } self::$handlers[$name][] = $callback; } public static function run( $event, array $args = array() ) { foreach ( self::$handlers[$event] as $hook ) { if ( !is_array( $hook ) ) { $hook = array( $hook ); } if ( !array_filter( $hook ) ) { continue; } elseif ( is_array( $hook[0] ) ) { $hook = array_merge( $hook[0], array_slice( $hook, 1 ) ); } if ( $hook[0] instanceof Closure ) { $func = "hook-$event-closure"; $callback = array_shift( $hook ); } elseif ( is_object( $hook[0] ) ) { $object = array_shift( $hook ); $method = array_shift( $hook ); if ( $method === null ) { $method = "on$event"; } $func = get_class( $object ) . '::' . $method; $callback = array( $object, $method ); } elseif ( is_string( $hook[0] ) ) { $func = $callback = array_shift( $hook ); } $retval = null; $badhookmsg = null; $hook_args = array_merge( $hook, $args ); $retval = call_user_func_array( $callback, $args ); } } } Hooks::register( 'foo', function( &$foo, &$var ) { $var = $var . '-bar'; } ); //Stub class using &$this as direct reference class Foo { public function doSomething( $var ) { Hooks::run( 'foo', array( &$this, &$var ) ); return $var; } } // Stub class causes no error after $this is being redeclared class Foo2 { public function doSomething( $var ) { $foo = $this; Hooks::run( 'foo', array( &$foo, &$var ) ); return $var; } } // Causes an error $fooA = new Foo(); $varA = $fooA->doSomething( '123' ); echo "Foo: \n"; echo $varA . "\n"; // Causes no error $fooB = new Foo2(); $varB = $fooB->doSomething( '123' ); echo "Foo2: \n"; echo $varB;
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.25, 7.0.0 - 7.0.20, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Foo: 123-bar Foo2: 123-bar
Output for 7.1.0 - 7.1.20, 7.2.0 - 7.2.33
Warning: Parameter 1 to {closure}() expected to be a reference, value given in /in/vMc1N on line 51 Foo: 123-bar Foo2: 123-bar
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_FUNCTION in /in/vMc1N on line 56
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE in /in/vMc1N on line 17
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/vMc1N on line 7
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
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/vMc1N on line 7
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/vMc1N on line 7
Process exited with code 255.

preferences:
248.86 ms | 401 KiB | 352 Q