dotxxh0998 2016-12-06 21:02
浏览 19
已采纳

为什么在实体构造函数中使用setter或field键会得到不同的结果?

Here is a dump of a new instance of my entity Theme, when using $this->type = 'theme'; in the __construct function.

ExplorerController.php on line 197:
Theme {#450 ▼
  -id: null
  -headings: ArrayCollection {#449 ▶}
  -infos: null
  -base: null
  -deletedAt: null
  -key: null
  -description: null
  -type: null
  +"type": "theme"
}

I don't understand why the last field type appears twice, first with a null value, and then between quotes with a "plus" sign in front.

If I use the setter $this->setType('theme'); then the result is as expected:

ExplorerController.php on line 197:
Theme {#450 ▼
  -id: null
  -headings: ArrayCollection {#449 ▶}
  -infos: null
  -base: null
  -deletedAt: null
  -key: null
  -description: null
  -type: "theme"
}

I guess it has to do with proxies, but this a subject I don't fully understand.

Could someone explain what's happening here?

  • 写回答

1条回答 默认 最新

  • douguangxiang0363 2016-12-06 21:18
    关注
    • A dash in front of type indicates it's a private member.
    • A plus indicates a public member.

    By explicitly setting $this->type, you are setting the public member.

    Without actually seeing your code, I cannot offer any other advice except to make sure you are not defining $type twice (maybe in an extended class or trait?).

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

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题