Hướng dẫn php unset protected property - php chưa đặt tài sản được bảo vệ

Tôi có một đối tượng/lớp sản phẩm như sau:

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;

}

Và vì vậy tôi có một đối tượng sản phẩm và tôi muốn xóa tất cả các thuộc tính của sản phẩm đó, vì vậy tôi đã làm:

unset(product->attributes)

Tuy nhiên, nó phàn nàn rằng các thuộc tính không thể được truy cập. Làm thế nào có thể đặt các thuộc tính của sản phẩm là nil?

Nếu tôi có một

unset(product->attributes)
1 nói,
unset(product->attributes)
2.

Chắc chắn không có vấn đề gì khi gán một tài sản mới,

unset(product->attributes)
2,

$a->new_property = $xyz;

Nhưng sau đó tôi muốn loại bỏ nó, vì vậy

unset(product->attributes)
4 không có ích ở đây.

So,

$a->new_property = null;

là loại của nó. Nhưng có một cách 'thanh lịch' hơn?

Yakovl

6.91012 Huy hiệu vàng57 Huy hiệu bạc88 Huy hiệu đồng12 gold badges57 silver badges88 bronze badges

Hỏi ngày 30 tháng 8 năm 2010 lúc 13:25Aug 30, 2010 at 13:25

2

unset($a->new_property);

Điều này hoạt động cho các phần tử mảng, biến và thuộc tính đối tượng.

Example:

$a = new stdClass();

$a->new_property = 'foo';
var_export($a);  // -> stdClass::__set_state(array('new_property' => 'foo'))

unset($a->new_property);
var_export($a);  // -> stdClass::__set_state(array())

Đã trả lời ngày 30 tháng 8 năm 2010 lúc 13:26Aug 30, 2010 at 13:26

Yanick Rochonyanick RochonYanick Rochon

48.7K24 Huy hiệu vàng122 Huy hiệu bạc195 Huy hiệu Đồng24 gold badges122 silver badges195 bronze badges

6

Điều này cũng hoạt động đặc biệt nếu bạn đang lặp qua một đối tượng.

unset($object[$key])

Cập nhật

Các phiên bản mới hơn của Php ném lỗi gây tử vong

unset(product->attributes)
5 như được đề cập bởi @cxj. Trong trường hợp đó, bạn có thể sử dụng dấu ngoặc thay thế

unset($object->{$key})

Grzegorz

5.3241 Huy hiệu vàng27 Huy hiệu bạc49 Huy hiệu đồng1 gold badge27 silver badges49 bronze badges

Đã trả lời ngày 9 tháng 9 năm 2014 lúc 15:07Sep 9, 2014 at 15:07

Sajjad Ashrafsajjad AshrafSajjad Ashraf

3.6241 Huy hiệu vàng33 Huy hiệu bạc34 Huy hiệu đồng1 gold badge33 silver badges34 bronze badges

2

Điều này cũng hoạt động nếu bạn đang lặp qua một đối tượng.

unset($object->$key);

Không cần sử dụng dấu ngoặc.

Đã trả lời ngày 28 tháng 2 năm 2019 lúc 15:49Feb 28, 2019 at 15:49

Dandybohdandybohdandyboh

1171 Huy hiệu bạc2 Huy hiệu đồng1 silver badge2 bronze badges

1

Mã này hoạt động tốt cho tôi trong một vòng lặp

$remove = array(
    "market_value",
    "sector_id"
);

foreach($remove as $key){
    unset($obj_name->$key);
}

Đã trả lời ngày 29 tháng 7 năm 2020 lúc 14:48Jul 29, 2020 at 14:48

Ashiq Deyashiq DeyAshiq Dey

2572 Huy hiệu bạc13 Huy hiệu Đồng2 silver badges13 bronze badges

1

Đặt một phần tử thành null chỉ cần đặt giá trị của phần tử thành null phần tử vẫn tồn tại

UndT Một phần tử có nghĩa là loại bỏ phần tử mà nó hoạt động cho mảng, các đối tượng std class, các lớp xác định người dùng và cũng cho bất kỳ biến nào

unset(product->attributes)
0

Đã trả lời ngày 25 tháng 7 năm 2021 lúc 18:05Jul 25, 2021 at 18:05

TexwillertexwillerTexWiller

3082 Huy hiệu bạc9 Huy hiệu Đồng2 silver badges9 bronze badges