2

I'm having some trouble using PHP object property names in a flexible way, similar to array keys.

My understanding is that an array key can be any string or integer, e.g

$arr = array("crash-me" => "value");

is a valid key/value pair.

Now if I "cast" that to an object:

$obj = (object) $arr;

have I done something illegal, because of the dash in the key string?

A simple example shows my dilema:

<?php
error_reporting(-1);
$obj = new stdClass;
$prop = "crash-me";
$arr = array();
$obj->$prop = "no crash"; 
$obj->$prop = $arr; // bp here and $obj->$prop is correct
print_r($obj); // bp here, inspect $obj->$prop and... xdebug/php will crash.
exit;

This will crash eclipse-pdt using xdebug when you inspect the value of the $obj before the print_r().

However, without bp, contents are printed correctly from print_r() at exit.

stdClass Object ( [crash-me] => Array ( ) ) 

Note that if I stuff something into the array when assigning it to the property, things are OK:

$obj = new stdClass;
$prop = "crash-me";
$arr = array("works_ok");
$obj->$prop = "no crash";
$obj->$prop = $arr;
print_r($obj);
exit;

Bottom-line, I'd like to use property names with dashes and possibly other punctuation similar to array key. Is it possible? Or am I doing something illegal by assigning an empty array, in which case wouldn't an error be caught?

Thanks ahead for your kind consideration!

on edit, I forgot another part of puzzle - the code below does not crash. In this case, the property name does not have a dash, however, the array key is empty!

$obj = new stdClass;
$prop = "crash_me";  // note underscore
$arr = array();
$obj->$prop = "no crash"; 
$obj->$prop = $arr; // bp here and $obj->$prop is correct
print_r($obj); // bp here, inspect $obj->$prop and... no crash!
exit;

Sorry for the confusion! It's probably something obvious I am missing.

On second edit-

To clarify, I was hoping to read property names from json_decode() and simply use the objects without having to convert to arrays. It was an experiment with dynamic code generation using keys from json data files. Per discussion below, looks like if I want to continue with PHP for this, I'll need the $arr['key1']['key2']... syntax and have the decoder use the "array" flag.

Thanks for the great replies!

1 Answers1

4

I 'm not sure if xdebug has any problems with non-standard property names, but you definitely do not want to use property names like these on purpose. It doesn't make sense.

If you intend to use objects for OO purposes then you will declare their properties formally. In this case it is flat out impossible to use "strange" property names and there is nothing more to say.

If you don't intend to use objects for OO then you simply end up having arrays into which you can index $like->this instead of $like['this']. While the first form is shorter and I do prefer it myself, it becomes much uglier when using properties $with->{'strange-names'} -- worse than the array access in fact. So again, there is nothing to be gained from doing this.

In some really exceptional situations there might be a case for property names like that -- for example if an object uses the names of its properties as column names in a database table. But even in such cases it is usually much more convenient to add a translation layer between PHP property names and database column names instead of fighting with the syntax every step of the way.

Bonus argument: If one look at this question is not enough to change your mind, nothing will. It's just a specific corner case, but we are talking about PHP. Corner cases should be... avoided.

Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
  • +1 '"strange" property names and there is nothing more to say.' –  Feb 15 '13 at 20:03
  • `SimpleXMLElement` uses property names which may not be legal PHP var names but which are legal XML element names. It's not always inappropriate to do this, but I wouldn't make a habit of it. – Francis Avila Feb 15 '13 at 20:03
  • OK, I was afraid of that. I am experimenting with some automation and thought it might be possible to dynamically assign property names. – user1501974 Feb 15 '13 at 20:06
  • @user1501974: I think you will find that there's simply nothing to gain vs just using arrays in situations like that. – Jon Feb 15 '13 at 20:08
  • @user1501974, to clarify, you *can* dynamically assign property names. We're arguing that you should probably just use arrays instead. As for the crashing, that seems like an xdebug issue. – Francis Avila Feb 15 '13 at 20:15
  • @Jon - BTW , I was experimenting with json and input is free form, so objects have property names that can have dashes after json_decode()... I was hoping to use that as a human readable format. – user1501974 Feb 15 '13 at 20:16
  • @user1501974: `json_decode` has an extra parameter that makes it return arrays instead of objects. You might find that more convenient. – Jon Feb 15 '13 at 20:18
  • @Jon, yes, I was a bit naive about the idea. Need to rethink... thanks for great reply! – user1501974 Feb 15 '13 at 20:22