PHP - Object and Array Creation + Dynamic Accessors

<?php /** ARRAY * * $an_array did NOT need be be defined ahead of time * before creating and adding a value to it. */ $array['is_valid'] = true; /** OBJECT * * $obj is required to be defined before using * but the property can be added on the fly. */ $obj = new stdClass(); $obj->{'is_valid'} = true; /** INVALID * * $bad_obj is required to be defined before using. * This will NOT work to auto create the object + set the property. */ $bad_obj->{'is_not_valid'} = '!'; // fail

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.