php object attribute with dot in name

10,677

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

Share:
10,677

Related videos on Youtube

egis
Author by

egis

Self-taught web developer.

Updated on May 25, 2022

Comments

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

    • Gordon
      Gordon about 13 years
      This 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
    Pekka about 13 years
    +1 but operation.date needs to be wrapped in quotes I think.
  • Gordon
    Gordon about 13 years
    +1 for telling the OP how to cure the cause and not the symptoms
  • egis
    egis about 13 years
    I 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
    Salvi Pascual over 8 years
    You need to wrap into quotes like echo $object->{'operation.date'}