<?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 ) );
You have javascript disabled. You will not be able to edit any code.