PHP에서 이름에 공백이 있는 클래스 속성에 액세스하는 방법은 무엇입니까?

Mary-Kate Olsen
풀어 주다: 2024-10-18 15:43:03
원래의
114명이 탐색했습니다.

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으로 문의하세요.
저자별 최신 기사
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!