3v4l.org

run code in 300+ PHP versions simultaneously
<?php class With_Magic_Methods { private $_prop = array(); protected $compat_fields = array( '_prop' ); public function __get( $name ) { if ( in_array( $name, $this->compat_fields, true ) ) { return $this->$name; } } public function __set( $name, $value ) { if ( in_array( $name, $this->compat_fields, true ) ) { return $this->$name = $value; } } public function __isset( $name ) { if ( in_array( $name, $this->compat_fields, true ) ) { return isset( $this->$name ); } return false; } public function __unset( $name ) { if ( in_array( $name, $this->compat_fields, true ) ) { unset( $this->$name ); } } } class No_Magic_Methods { public $_prop = array(); } function run_test() { $with_magic_methods = new With_Magic_Methods(); $no_magic_methods = new No_Magic_Methods(); echo "\n ***** TEST WITH A DYNAMIC PROPERTY ***** \n\n\n"; // __get() echo "** Test `__get()` **\n"; echo "With magic methods: "; var_dump( $with_magic_methods->_dynamic_property ); echo "No magic methods: "; var_dump( $no_magic_methods->_dynamic_property ); // __isset() echo "\n\n ** Test `__isset()` **\n"; echo "With magic methods: "; var_dump( isset( $with_magic_methods->_dynamic_property ) ); echo "No magic methods: "; var_dump( isset( $no_magic_methods->_dynamic_property ) ); // __set() echo "\n\n ** Test `__set()` **\n"; $new_value = array( 'test' ); $with_magic_methods->_dynamic_property = $new_value; $no_magic_methods->_dynamic_property = $new_value; echo "With magic methods: "; var_dump( $with_magic_methods->_dynamic_property ); echo "No magic methods: "; var_dump( $no_magic_methods->_dynamic_property ); // Unset. unset( $with_magic_methods->_dynamic_property ); unset( $no_magic_methods->_dynamic_property ); echo "\n\n ** Test `__isset()` after an `__unset()` **\n"; echo "With magic methods:"; var_dump( isset( $with_magic_methods->_dynamic_property ) ); echo "No magic methods: "; var_dump( isset( $no_magic_methods->_dynamic_property ) ); echo "\n\n ** Test `__get()` after an __unset() **\n"; echo "With magic methods:"; var_dump( $with_magic_methods->_dynamic_property ); echo "No magic methods: "; var_dump( $no_magic_methods->_dynamic_property ); echo "\n\n ** Test `__set()` after an __unset() **\n"; $new_value = array( 'test after an unset' ); $with_magic_methods->_dynamic_property = $new_value; $no_magic_methods->_dynamic_property = $new_value; echo "With magic methods:"; var_dump( $with_magic_methods->_dynamic_property ); echo "No magic methods: "; var_dump( $no_magic_methods->_dynamic_property ); } run_test();
Output for 8.2.0 - 8.2.27, 8.3.0 - 8.3.15, 8.4.1 - 8.4.2
***** TEST WITH A DYNAMIC PROPERTY ***** ** Test `__get()` ** With magic methods: NULL No magic methods: Warning: Undefined property: No_Magic_Methods::$_dynamic_property in /in/nCACX on line 49 NULL ** Test `__isset()` ** With magic methods: bool(false) No magic methods: bool(false) ** Test `__set()` ** Deprecated: Creation of dynamic property No_Magic_Methods::$_dynamic_property is deprecated in /in/nCACX on line 62 With magic methods: NULL No magic methods: array(1) { [0]=> string(4) "test" } ** Test `__isset()` after an `__unset()` ** With magic methods:bool(false) No magic methods: bool(false) ** Test `__get()` after an __unset() ** With magic methods:NULL No magic methods: Warning: Undefined property: No_Magic_Methods::$_dynamic_property in /in/nCACX on line 82 NULL ** Test `__set()` after an __unset() ** Deprecated: Creation of dynamic property No_Magic_Methods::$_dynamic_property is deprecated in /in/nCACX on line 87 With magic methods:NULL No magic methods: array(1) { [0]=> string(19) "test after an unset" }
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.31
***** TEST WITH A DYNAMIC PROPERTY ***** ** Test `__get()` ** With magic methods: NULL No magic methods: Warning: Undefined property: No_Magic_Methods::$_dynamic_property in /in/nCACX on line 49 NULL ** Test `__isset()` ** With magic methods: bool(false) No magic methods: bool(false) ** Test `__set()` ** With magic methods: NULL No magic methods: array(1) { [0]=> string(4) "test" } ** Test `__isset()` after an `__unset()` ** With magic methods:bool(false) No magic methods: bool(false) ** Test `__get()` after an __unset() ** With magic methods:NULL No magic methods: Warning: Undefined property: No_Magic_Methods::$_dynamic_property in /in/nCACX on line 82 NULL ** Test `__set()` after an __unset() ** With magic methods:NULL No magic methods: array(1) { [0]=> string(19) "test after an unset" }
Output for 5.2.6 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
***** TEST WITH A DYNAMIC PROPERTY ***** ** Test `__get()` ** With magic methods: NULL No magic methods: Notice: Undefined property: No_Magic_Methods::$_dynamic_property in /in/nCACX on line 49 NULL ** Test `__isset()` ** With magic methods: bool(false) No magic methods: bool(false) ** Test `__set()` ** With magic methods: NULL No magic methods: array(1) { [0]=> string(4) "test" } ** Test `__isset()` after an `__unset()` ** With magic methods:bool(false) No magic methods: bool(false) ** Test `__get()` after an __unset() ** With magic methods:NULL No magic methods: Notice: Undefined property: No_Magic_Methods::$_dynamic_property in /in/nCACX on line 82 NULL ** Test `__set()` after an __unset() ** With magic methods:NULL No magic methods: array(1) { [0]=> string(19) "test after an unset" }
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.5
***** TEST WITH A DYNAMIC PROPERTY ***** ** Test `__get()` ** With magic methods: NULL No magic methods: Notice: Undefined property: No_Magic_Methods::$_dynamic_property in /in/nCACX on line 49 NULL ** Test `__isset()` ** With magic methods: bool(false) No magic methods: bool(false) ** Test `__set()` ** With magic methods: NULL No magic methods: array(1) { [0]=> string(4) "test" } ** Test `__isset()` after an `__unset()` ** With magic methods:bool(false) No magic methods: bool(false) ** Test `__get()` after an __unset() ** With magic methods:NULL No magic methods: Notice: Undefined property: No_Magic_Methods::$_dynamic_property in /in/nCACX on line 82 NULL ** Test `__set()` after an __unset() ** With magic methods:NULL No magic methods: array(1) { [0]=> string(19) "test after an unset" }
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/nCACX 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
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/nCACX on line 4
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/nCACX on line 4
Process exited with code 255.

preferences:
62.64 ms | 417 KiB | 5 Q