3v4l.org

run code in 300+ PHP versions simultaneously
<?php / returns true if property is safely accessible publicly by using $obj->$prop: function public_property_exists( $obj, $prop ){ / allow magic $obj->__isset( $prop ) to execute if exists if( isset( $obj->$prop )) return true; / no public/protected/private property exists with this name if( ! property_exists( $obj, $prop )) return false; / the property exists, but is it public? $rp = new ReflectionProperty( $obj, $prop ); return $rp->isPublic(); } class C { public $public = "I’m public!"; protected $protected = "I’m public!"; private $private = "I’m public!"; function __isset( $k ){ return substr( $k, 0, 5 ) === 'magic'; } function __get( $k ){ if( $k === 'magic_isset_but_null') return null; return "I’m {$k}!"; } } $o = new C(); foreach( array( 'public', 'protected', 'private', 'magic', 'magic_isset_but_null', 'missing' ) as $prop ){ if( public_property_exists( $o, $prop )) echo "\$o->{$prop} is a public property, its value is: ", var_export( $o->$prop, true ), "\n"; else echo "\$o->{$prop} is not a public property.\n"; }
Output for 5.1.0 - 5.1.6, 5.2.0 - 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, 8.0.0 - 8.0.30, 8.1.0 - 8.1.32, 8.2.0 - 8.2.28, 8.3.0 - 8.3.20, 8.4.1 - 8.4.6
$o->public is a public property, its value is: 'I’m public!' $o->protected is not a public property. $o->private is not a public property. $o->magic is a public property, its value is: 'I’m magic!' $o->magic_isset_but_null is a public property, its value is: NULL $o->missing is not a public property.
Output for 5.0.0 - 5.0.5
$o->public is a public property, its value is: 'I’m public!' Fatal error: Cannot access protected property C::$protected in /in/QBTd1 on line 6
Process exited with code 255.
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/QBTd1 on line 17
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/QBTd1 on line 17
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/QBTd1 on line 17
Process exited with code 255.

preferences:
53.25 ms | 411 KiB | 5 Q

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant