php 如何取消设置/删除受保护属性

aiqt4smr  于 2022-12-02  发布在  PHP
关注(0)|答案(4)|浏览(113)

我有一个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?

yv5phkfx

yv5phkfx1#

根据定义,受保护的属性只能从定义它的类别的执行严修或子代中存取。
这就是为什么人们会选择将属性设置为protected而不是public,以使其无法在类的示例之外进行更改。
要取消设置它,必须在类中完成,即

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

3z6pesqy2#

您可以使用反映(Reflection)将私用/受保护的属性(Property)设定为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);
lfapxunr

lfapxunr3#

我想你得为它办一个公开活动:

class product{
    // Include everything from above

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

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

vmdwslir

vmdwslir4#

使用arra_diff代替unset,如下所示:

$this->channels = ['value_1', 'value_2'];

$this->channels = array_values(array_diff($this->channels, array('value_1')));

相关问题