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 "** Test `__get()` **\n"; echo "With magic methods: "; var_dump( $with_magic_methods->_prop ); echo "No magic methods: "; var_dump( $no_magic_methods->_prop ); echo "\n\n ** Test `__isset()` **\n"; echo "With magic methods: "; var_dump( isset( $with_magic_methods->_prop ) ); echo "No magic methods: "; var_dump( isset( $no_magic_methods->_prop ) ); echo "\n\n ** Test `__set()` **\n"; $new_value = array( 'test' ); $with_magic_methods->_prop = $new_value; $no_magic_methods->_prop = $new_value; echo "With magic methods: "; var_dump( $with_magic_methods->_prop ); echo "No magic methods: "; var_dump( $no_magic_methods->_prop ); echo "\n\n ** Test `__isset()` after an `__unset()` **\n"; unset( $with_magic_methods->_prop ); unset( $no_magic_methods->_prop ); echo "With magic methods:"; var_dump( isset( $with_magic_methods->_prop ) ); echo "No magic methods: "; var_dump( isset( $no_magic_methods->_prop ) ); echo "\n\n ** Test `__get()` after an __unset() **\n"; echo "With magic methods:"; var_dump( $with_magic_methods->_prop ); echo "No magic methods: "; var_dump( $no_magic_methods->_prop ); echo "\n\n ** Test `__set()` after an __unset() **\n"; $new_value = array( 'test after an unset' ); $with_magic_methods->_prop = $new_value; $no_magic_methods->_prop = $new_value; echo "With magic methods:"; var_dump( $with_magic_methods->_prop ); echo "No magic methods: "; var_dump( $no_magic_methods->_prop ); } run_test();
Output for 8.1.20 - 8.1.31, 8.2.7 - 8.2.27, 8.3.0 - 8.3.15, 8.4.1 - 8.4.2
** Test `__get()` ** With magic methods: array(0) { } No magic methods: array(0) { } ** Test `__isset()` ** With magic methods: bool(true) No magic methods: bool(true) ** Test `__set()` ** With magic methods: array(1) { [0]=> string(4) "test" } 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: Warning: Undefined property: With_Magic_Methods::$_prop in /in/jNFmL on line 9 NULL No magic methods: Warning: Undefined property: No_Magic_Methods::$_prop in /in/jNFmL on line 76 NULL ** Test `__set()` after an __unset() ** With magic methods:array(1) { [0]=> string(19) "test after an unset" } No magic methods: array(1) { [0]=> string(19) "test after an unset" }

preferences:
59.4 ms | 408 KiB | 5 Q