Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Class Properties with Spaces

stdClass Object ([Sector] => Manufacturing [Date Found] => 2010-05-03 08:15:19)

So I can access [Sector] by using $object->Sector but how can I access [Date Found] ?

like image 411
Kemal Fadillah Avatar asked Aug 11 '11 14:08

Kemal Fadillah


People also ask

How do you access object properties with spaces?

Use bracket notation to access a key that contains a space in an object, e.g. obj['my key'] . The bracket [] notation syntax allows us to access an object's key, even if it contains spaces, hyphens or special characters.

Can object Key have spaces?

Object Keys Are Strings Object keys with underscores and camelCase (no spaces) don't require the bracket syntax, but object keys with spaces and hyphens do. You may prefer the code readability of the . dot syntax, in which case you'll want to avoid spaces in your object keys: you might write object.

How do you declare and access properties of a class?

Note: A property declared without a Visibility modifier will be declared as public . Within class methods non-static properties may be accessed by using -> (Object Operator): $this->property (where property is the name of the property). Static properties are accessed by using the :: (Double Colon): self::$property .


2 Answers

You can do it this way:

$object->{'Date Found'} 
like image 190
Imi Borbas Avatar answered Sep 28 '22 10:09

Imi Borbas


have you tried

$property = 'Date Found'; $object->{$property}; 

Or simply

$object->{'Date Found'}; 
like image 31
Nemanja Avatar answered Sep 28 '22 08:09

Nemanja