dongyan7172 2016-08-25 03:07
浏览 27
已采纳

PHP是否允许在stdObject中包含重复的属性?

I'm stuck with a very weird bug. I have an object called $row that looks like this:

stdClass Object
(
    [title] => Some Title
    [body] => My body
    [topic] => Topic
    [dataType] => Survey
    [csvrownum] => 1
)

I'm just trying to print out the title property in the following way:

print_r($row->title);

However for some reason that doesn't output anything.

Then I've tried to manually set the title property and print it right after, something like this:

$row->title = 'My Title';
print_r($row->title);

Surprisingly it worked but why? To make this more strange I decided to var_dump the object after set the title variable by hand:

$row->title = 'My Title';
var_dump($row);

And this is what I've got:

class stdClass#391 (6) {
  public $title =>
  string(3) "Some title"
  public $body =>
  string(7) "My body"
  public $topic =>
  string(6) "Topic"
  public $dataType =>
  string(17) "Survey"
  public $csvrownum =>
  int(1)
  public $title =>
  string(8) "My title"
}

Notice the title key is duplicated with different values. Is there any condition under this could happen?

  • 写回答

1条回答 默认 最新

  • dongzhaiqiang6108 2016-08-25 03:22
    关注

    No, PHP does not allow an object to have duplicate property names, because objects in PHP are implemented just like arrays. They are both implemented as ordered hashmaps. In a hashmap, two things that have the same hash, overwrite each other.

    You likely just have unprintible characters in your object property name. You can see this more clearly by doing something like the following for debug purposes...

    foreach($row as $key => $value) {
        var_dump($key);
    }
    

    If we had an object like this, for example, you'd see it gets overwritten.

    $row = new stdClass;
    $row->title = "First";
    $row->title = "Second";
    

    But something like this might be more deceptive...

    $row = new stdClass;
    $row->{"title\0"} = "First";
    $row->title = "Second";
    

    Output from the foreach using var_dump on the key, would reveal this...

    string(6) "title"
    string(5) "title"
    

    Notice one is string of length 6 and the other is a string of length 5.

    Grain of salt

    It's always better to use var_dump when attempting to debug variables than using something like print_r, as var_dump was specifically designed for debug purposes, whereas print_r is just a recursive print (hence the name). Printing values like null, false, or empty strings, gives you no useful information for debug purposes, but var_dump does.

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

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line