如何取消/删除受保护的属性

时间:2013-06-10 04:02:43

标签: php

我有一个Product对象/类,如下所示:

class Product
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @Exclude()
     * @ORM\Column(name="deletedAt", type="datetime", nullable=true)
     */
    private $deletedAt;

    /**
     * @Assert\NotBlank()
     * @Assert\MinLength( limit=3, message=" Product Name should have at least {{ limit }} characters.")
     * @ORM\Column(name="name", type="string", length=100 , nullable=false)
     */
    protected $name;

    /**
     * @var datetime $created
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(type="datetime")
     */
    private $created;

    /**
     * @var datetime $updated
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(type="datetime")
     */
    private $updated;

    /**
     * @ORM\Column(name="description", type="string", length=350)
     */
    protected $description;


    /**
     * @Assert\NotBlank()
     * @ORM\Column(name="code", type="string", length=100)
     */
    protected $code;


    /**
     * @Assert\NotBlank()
     * @ORM\ManyToOne(targetEntity="Shopious\MainBundle\Entity\Category", inversedBy="products")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id", onDelete="CASCADE", nullable=true)
     */
    protected $category;

    /**
     * @Assert\NotBlank()
     * @Assert\Min(limit = "0", message = "negative number is invalid")
     * @Assert\Type(type="float", message="The value {{ value }} is not a valid number.")
     * @ORM\Column(name="price", type="float")
     */
    protected $price;

    /**
     * @Assert\NotBlank()
     * @Assert\Min(limit = "0", message = "negative number is invalid")
     * @Assert\Type(type="float", message="The value {{ value }} is not a valid number.")
     * @ORM\Column(name="height", type="float")
     */
    protected $height;

    /**
     * @Assert\NotBlank()
     * @Assert\Min(limit = "0", message = "negative number is invalid")
     * @Assert\Type(type="float", message="The value {{ value }} is not a valid number.")
     * @ORM\Column(name="width", type="float")
     */
    protected $width;

    /**
     * @Assert\NotBlank()
     * @Assert\Min(limit = "0", message = "negative number is invalid")
     * @Assert\Type(type="float", message="The value {{ value }} is not a valid number.")
     * @ORM\Column(name="length", type="float")
     */
    protected $length;

    /**
     * @Assert\NotBlank()
     * @Assert\Min(limit = "0", message = "negative number is invalid")
     * @Assert\Type(type="float", message="The value {{ value }} is not a valid number.")
     * @ORM\Column(name="weight", type="float" )
     */
    protected $weight;

    /**
     * @Accessor(getter="getShopRef")
     * @ORM\ManyToOne(targetEntity="Shopious\MainBundle\Entity\Shop", inversedBy="products")
     * @ORM\JoinColumn(name="shop_id", referencedColumnName="id", onDelete="CASCADE" , nullable=false)
     */
    protected $shop;


    /**
     * @ORM\OneToMany(targetEntity="ProductAttribute", mappedBy="product", cascade={"persist","remove"})
     */
    protected $attributes;

}

所以我有一个Product对象,我想删除该产品的所有属性,所以我做了:

unset(product->attributes)

然而它抱怨无法访问这些属性。如何将Product的属性设置为nil?

3 个答案:

答案 0 :(得分:4)

根据定义,只能从定义的类的实例或后代中访问受保护的属性。

这就是为什么人们会选择保护一个属性而不是公共属性,以使其无法在类的实例之外进行更改。

要取消设定,必须在课堂内进行,即

  public function removeAttributes(){
      unset($this->attributes);
  }

答案 1 :(得分:3)

您可以使用反射将私有/受保护的属性设置为null

http://php.net/manual/en/reflectionproperty.setvalue.php

class Foo {
    public static $staticProperty;

    public $property;
    protected $privateProperty;
}

$reflectionClass = new ReflectionClass('Foo');

$reflectionProperty = $reflectionClass->getProperty('privateProperty');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($foo, null);

答案 2 :(得分:2)

我认为你必须为它做一个公共功能:

class product{
    // Include everything from above

    public function unset_attributes(){
        unset($this->attributes);
    }
}

有关对象内受保护变量的更多信息,请参见here