ホームページ  >  記事  >  バックエンド開発  >  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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。