首页 > 后端开发 > php教程 > 如何在 PHP 中访问名称中带有空格的类属性?

如何在 PHP 中访问名称中带有空格的类属性?

Mary-Kate Olsen
发布: 2024-10-18 15:43:03
原创
197 人浏览过

How to Access Class Properties with Spaces in Their Names in PHP?

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.

<code class="php">$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</code>
登录后复制

This approach allows you to access properties with spaces or other special characters that would otherwise cause syntax errors.

以上是如何在 PHP 中访问名称中带有空格的类属性?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板