dongyuan8469 2013-02-15 19:55
浏览 34
已采纳

需要帮助理解php对象属性名称与数组键

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条回答 默认 最新

  • douzi4766 2013-02-15 20:01
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算