为什么在实体构造函数中使用setter或field key会给出不同的结果?

为什么在实体构造函数中使用setter或field key会给出不同的结果?

问题描述:

__construct函数中使用$this->type = 'theme';时,下面是我的实体Theme的新实例的转储。为什么在实体构造函数中使用setter或field key会给出不同的结果?

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" 
} 

我不明白为什么最后一个字段出现了两次,先用null值,然后在前面的“+”号引号之间。

如果我使用的setter $this->setType('theme');则如预期的结果:

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

我猜它与代理的事,但这样的问题,我不完全理解。

有人可以解释这里发生了什么?

+0

您能否为我们提供您的实体代码? – olibiaz

  • type前面的破折号表示它是私人会员。
  • A plus表示公共成员。

通过明确设置$this->type,您正在设置公共成员。

没有真正看到你的代码,除了确保你没有定义$type两次(也许在扩展类或特征?)之外,我不能提供任何其他建议。

+0

我的类“实体”从**映射的超类**继承字段“类型”。但它没有被定义两次,并且在'__construct'函数中使用setter时,行为与预期相同。 (请参阅我的编辑) – Roubi

+0

属性_defined_有一次(在父类中),但在子类中添加了具有相同名称的属性。在子类中,您无法访问父项的私有属性,所以当您在子项中设置'$ this-> type'时,您将添加新的动态属性(具有相同名称的'类型')。 – Timurib

+2

请参阅https://3v4l.org/u3Uqh – Timurib