3v4l.org

run code in 500+ PHP versions simultaneously
<?php class Outer { private static $inner = null; private function __construct() { } private static function ensure() { if ( isset( self::$inner ) ) { return; } self::$inner = new class () { private $html = ''; public static function wrap( $html ) { $self = new self(); $self->html = $html; return $self; } public static function unwrap( $self ) { return $self->html; } }; } public static function wrap( $html ) { self::ensure(); return self::$inner::wrap( $html ); } public static function unwrap( $self ) { return $self instanceof self::$inner ? self::$inner::unwrap( $self ) : $self; } } $wrapped = Outer::wrap( '<div>just <script>a test</script> some HTML</div>' ); var_dump( $wrapped ); var_dump( Outer::unwrap( $wrapped ) ); // Can’t call this from outside `Outer` because of the anonymous class. // var_dump( $wrapped->unwrap() ); // It’s still possible to call via a self-reference, but that’s awkward and will stand out. var_dump( $wrapped::unwrap( $wrapped ) );
Output for 8.6.0
object(class@anonymous/in/aOOj7:15$0)#2 (1) { ["html":"class@anonymous":private]=> string(49) "<div>just <script>a test</script> some HTML</div>" } string(49) "<div>just <script>a test</script> some HTML</div>" string(49) "<div>just <script>a test</script> some HTML</div>"
Output for 8.2.0 - 8.2.36, 8.3.0 - 8.3.37, 8.4.1 - 8.4.28, 8.5.0 - 8.5.13
object(class@anonymous)#2 (1) { ["html":"class@anonymous":private]=> string(49) "<div>just <script>a test</script> some HTML</div>" } string(49) "<div>just <script>a test</script> some HTML</div>" string(49) "<div>just <script>a test</script> some HTML</div>"

preferences:
27.74 ms | 1168 KiB | 6 Q