首頁  >  文章  >  後端開發  >  如何在 PHP 中存取名稱中帶有空格的類別屬性?

如何在 PHP 中存取名稱中帶有空格的類別屬性?

Mary-Kate Olsen
Mary-Kate Olsen原創
2024-10-18 15:43:03115瀏覽

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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn