Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP colon in property name

Tags:

arrays

php

I have a variable $entry which is set to:

stdClass Object ( [im:name] => stdClass Object ( [label] => Amazing Breaker ) etc.

How can I access the value "Amazing Breaker"?

I tried to define a constant:

define("IMNAME", 'im:name');

but using:

foreach ($json_output->feed->entry as $entry) {
if (isset($entry->IMNAME->label))

returns FALSE.

Problem seems to be the colon. For a key without colon the code would return TRUE.

like image 477
Timothée HENRY Avatar asked Nov 21 '11 11:11

Timothée HENRY


People also ask

What does a colon mean in PHP?

In PHP, the double colon :: is defined as Scope Resolution Operator. It used when when we want to access constants, properties and methods defined at class level. When referring to these items outside class definition, name of class is used along with scope resolution operator.

What does => mean in PHP?

=> is the separator for associative arrays. In the context of that foreach loop, it assigns the key of the array to $user and the value to $pass .

What is@ property in PHP?

@property shows a "magic" property variable that is found inside the class. datatype should be a valid PHP type or "mixed." phpDocumentor will display the optional description unmodified, and defaults to "mixed" if the datatype is not present.

What does this -> do in PHP?

This is referred to as the object operator, or sometimes the single arrow operator. It is an access operator used for access/call methods and properties in a PHP object in Object-Oriented Programming (OOP). Example.


1 Answers

You can use the free-form syntax with curly braces for 'special' property names:

if (isset($entry->{'im:name'}->label))
like image 164
mario Avatar answered Sep 19 '22 15:09

mario