Accessing Properties with Spaces in PHP
In PHP, accessing class properties with spaces in their names can be challenging. Consider a stdClass object with properties "Sector" and "Date Found". While you can access "Sector" using $object->Sector, how do you retrieve the value for "Date Found"?
To access a property with a space in the name, use curly braces around the property name. For instance, in this case, you would use $object->{'Date Found'} to obtain the date found value.
$object = new stdClass();
$object->{'Sector'} = 'Manufacturing';
$object->{'Date Found'} = '2010-05-03 08:15:19';
echo $object->{'Date Found'}; // Output: 2010-05-03 08:15:19
This approach allows you to access properties with spaces or other special characters that would otherwise cause syntax errors.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3