Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access an object property with a minus-sign?

Tags:

object

php

I got an object (in PHP) and I can't print the content. In debug-mode it's like this:

stdClass Object
(
    [pre-selection] => 1
)

But I can't print the 'pre-selection' because of the minus sign.

echo $object->pre-selection; //doens't work.

How can I print this out? Thanks.

like image 443
Kevin Gorjan Avatar asked Aug 13 '12 18:08

Kevin Gorjan


People also ask

How do you access the object property of a dash?

Use bracket notation to access an object property with a hyphen, e.g. obj['with-hyphen'] . There are two ways to access properties on an object - dot notation and bracket notation. If the property contains a hyphen, space, or special symbols, you have to use bracket notation.

How do you access object properties in TypeScript?

To dynamically access an object's property: Use keyof typeof obj as the type of the dynamic key, e.g. type ObjectKey = keyof typeof obj; . Use bracket notation to access the object's property, e.g. obj[myVar] .

What are the two types of notations does object property have?

There are two ways to access properties: dot notation and bracket notation.

What notation do you use to access an object inside another object in JavaScript?

Bracket notation is another way to access a property of an object. To use bracket notation, write the name of the object, followed by brackets [] . Inside the brackets, write the property name as a string. Bracket notation, unlike dot notation, can be used with variables.


2 Answers

You could try

$object->{'pre-selection'};

http://php.net/manual/en/language.types.string.php#language.types.string.parsing.complex

See also Example 2 of json_decode()

Example #2 Accessing invalid object properties

Accessing elements within an object that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.

<?php

$json = '{"foo-bar": 12345}';

$obj = json_decode($json);
print $obj->{'foo-bar'}; // 12345

?>

Update (thanks to salathe):

Curly braces may also be used, to clearly delimit the property name. They are most useful when accessing values within a property that contains an array, when the property name is made of mulitple parts, or when the property name contains characters that are not otherwise valid

like image 162
Steve Robbins Avatar answered Sep 20 '22 09:09

Steve Robbins


There are multiple ways, the problem is that the PHP tokenizer will choke on the - sign in the code, howver you can write it so that the parser does not complains:

echo $object->{'pre-selection'};

or

$property = 'pre-selection'
echo $object->$property;

or

$array = (array) $object;   
echo $array['pre-selection'];

In these cases, the PHP parser does not run about a place in the raw code that it has a problem to parse with any longer.


Wondering where this is documented. For example in the SimpleXML documentation:

Accessing elements within an XML document that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.

Example #3 Getting <line>

<?php
include 'example.php';

$movies = new SimpleXMLElement($xmlstr);

echo $movies->movie->{'great-lines'}->line;
?>

The above example will output:

PHP solves all my web problems
like image 39
hakre Avatar answered Sep 22 '22 09:09

hakre