如何更新我的私有布尔值 属性?

How can I update my private boolean property?

我的 "fields" 实体:

  /**
  * @ORM\Column(type="boolean", nullable=true)
  */
  private $color;




public function getColor(): ?bool
  {
    return $this->color;
  }

  public function setColor(?bool $color): self
  {
    $this->color = $color;

    return $this;
  }

我很难更新我的私人 属性:

$field = "color"
$content = 1;
$value->{$field} = $content;

我收到错误消息:

Cannot access private property App\Entity\Fields::$color

所以我测试了这个:

$value->setColor(1);

但我收到错误消息:

Attempted to call an undefined method named "setColor" of class "stdClass".

一切正常,我将 private 更改为 public。 但我就是不知道如何用私有 属性.

设置值

此代码:

<?php
class MyClass{
    private $attribute;

    public function getAttribute(){
        return $this->attribute;
    }

    public function setAttribute(?int $value){
        if($value)
        $this->attribute = $value;
    }
}

$obj = new MyClass;

//$obj->attribute = 3; //this won't work because attribute is private

$obj->setAttribute(42);

echo $obj->getAttribute();

按预期工作并输出 42;尝试为您的 class 创建一个新对象并执行相同的操作,看看它是否有效。