3v4l.org

run code in 300+ PHP versions simultaneously
<?php // This is a basic class without any special behavior. No deprecation error occur despite the public_property being unset. $foo = new Foo(); $foo->public_property = true; unset( $foo->public_property ); $foo->public_property = true; // A class with a __set magic method that allows modifying declared public properties, mimicking the basic class behaviour and providing more backward compatibility. $baz = new Baz(); $baz->public_property = true; unset( $baz->public_property ); $baz->public_property = true; // This is a class with a __set magic method that restricts dynamic property creation. $bar = new Bar(); $bar->public_property = true; unset( $bar->public_property ); $bar->public_property = true; class Foo { public $public_property; } class Baz { public $public_property; public function __set( $prop, $value ) { if ( 'known_dynamic_property' === $prop ) { // Dynamic property-specific logic goes here. return; } // Checks if the property is a known public property and allows setting it. if ( 'public_property' === $prop ) { $this->public_property = $value; return; } trigger_error( sprintf( 'Setting the dynamic property "%s" on %s is deprecated', $prop, __CLASS__ ), E_USER_DEPRECATED ); } } class Bar { public $public_property; public function __set( $prop, $value ) { if ( 'known_dynamic_property' === $prop ) { // Dynamic property-specific logic goes here. return; } trigger_error( sprintf( 'Setting the dynamic property "%s" on %s is deprecated', $prop, __CLASS__ ), E_USER_DEPRECATED ); } }
Output for 8.2.17 - 8.2.25, 8.3.4 - 8.3.13
Deprecated: Setting the dynamic property "public_property" on Bar is deprecated in /in/QU0tT on line 57
Output for 8.1.28 - 8.1.30
Deprecated: Setting the dynamic property "public_property" on Bar is deprecated in /in/QU0tT on line 59

preferences:
48.42 ms | 407 KiB | 5 Q