php object attribute with dot in name
Solution 1
Specify aliases in your SQL query like SELECT column AS nameWithoutDots ...
or access these properties with $object->{'operation.name'}
or cast the object to array like this: $obj = (array)$obj; echo $obj['operation.name']
.
Solution 2
The correct way of accessing properties with a dot should be :
echo $object->{"operation.date"}
Solution 3
To access these attributes you need to wrap them with curly brackets:
echo $object->{"operation.date"} //2010-12-15
If you set an attribute this way the offending symbol gets removed, allowing you to access the attribute as echo $object->operationdate //2010-12-15
Solution 4
Change the sql to return valid property names using the 'as' feature
eg. select operation.date as date
Related videos on Youtube
Comments
-
egis almost 2 years
I have mysql table with collumns like 'operation.date', 'operation.name' and etc. After fetching that table data as object with
$mysqli->fetch_object()
i get this (print_r of row):stdClass Object ( [id] => 2 [operation.date] => 2010-12-15 [operation.name] => some_name )
how do I acces
operation.date
andoperation.name
and all other weirdly named object properties?-
Gordon about 13 yearsThis is a duplicate. The answer is, it's not a valid property. You should change it. But you can use
$obj->{'operation.date'}
to access it.
-
-
Pekka about 13 years+1 but
operation.date
needs to be wrapped in quotes I think. -
Gordon about 13 years+1 for telling the OP how to cure the cause and not the symptoms
-
egis about 13 yearsI know about aliases, but too much effort to write miles long query with all the columns as aliases :) Since your answer was most informative I will accept it ;) Thanks for all the answers.
-
Salvi Pascual over 8 yearsYou need to wrap into quotes like echo $object->{'operation.date'}