Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php object attribute with dot in name

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 and operation.name and all other weirdly named object properties?

like image 228
egis Avatar asked Mar 18 '11 11:03

egis


4 Answers

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'].

like image 141
rik Avatar answered Oct 19 '22 10:10

rik


The correct way of accessing properties with a dot should be :

echo $object->{"operation.date"}
like image 15
peipei Avatar answered Oct 19 '22 10:10

peipei


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

like image 5
Richard Parnaby-King Avatar answered Oct 19 '22 12:10

Richard Parnaby-King


Change the sql to return valid property names using the 'as' feature

eg. select operation.date as date

like image 2
Shaun Hare Avatar answered Oct 19 '22 12:10

Shaun Hare