How to Delete an Object's Property in PHP

Is it possible to delete an object's property in PHP?

unset($a->new_property);

This works for array elements, variables, and object attributes.

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())

How to remove property from object in PHP 7.4+

According to the RFC on typed properties:

If a typed property is unset(), then it returns to the uninitialized state. While we would love to remove support for the unsetting of properties, this functionality is currently used for lazy initialization by Doctrine, in combination with the functionality described in the following section.

Removing an item from object in a loop

Thats expected behaviour. There are two main way of doing what you want

foreach ($products as $key => $product) {

if(!$product->active) {
unset($products[$key]);
}

}

Second way would be to use reference

foreach ($products as &$product) {

if(!$product->active) {
unset($product);
}

}

Unsetting properties in an object

Instead of:

unset($curdel[$key]);

Try:

unset($curdel->$key);

Arrays are accessed/modified via the [] but objects and class properties are accessed via the ->

Remove a member from an object?

You are using RedBean. Just checked it out. And these bean objects don't have actual properties.

unset($bean->field);

Does not work, because ->field is a virtual attribute. It does not exist in the class. Rather it resides in the protected $bean->properties[] which you cannot access. RedBean only implements the magic methods __get and __set for retrieving and setting attributes.

This is why the unset() does not work. It unsets a property that never existed at that location.

Can't delete object property in php

You really should have posted your Session class in your post instead of linking to your GitHub repo... that's why the comments are confusing. You are using magic methods on your session class.

1 change I made: adding the magic __unset method.

Also, I had thought the constructor needed to be public but on further looking at it I was wrong about that (so my test code will not work unless the constructor is public... anyway...).

Here is the code below with the updated class:

<?
class Session {
public $storage;
public $token;
public $username;
public $browser;
public $start;
public $last_access;
private function __construct($u, $t, $s = null, $b = null, $d = null) {
$this->storage = $s ? $s : new stdClass();
$this->username = $u;
$this->token = $t;
$this->browser = $b ? $b : $_SERVER['HTTP_USER_AGENT'];
$this->start = $d ? $d : date('r');
}
function &__get($name) {
return $this->storage->$name;
}
function __set($name, $value) {
$this->storage->$name = $value;
}
function __isset($name) {
return isset($this->storage->$name);
}
function __unset($name) {
echo "Unsetting $name";
unset($this->storage->$name);
}
static function create_sessions($sessions) {
$result = array();
foreach ($sessions as $session) {
$result[] = new Session($session->username,
$session->token,
$session->storage,
$session->browser,
$session->start);
}
return $result;
}
static function cast($stdClass) {
$storage = $stdClass->storage ? $stdClass->storage : new stdClass();
return new Session($stdClass->username,
$stdClass->token,
$storage,
$stdClass->browser,
$stdClass->start);
}
static function new_session($username) {
return new Session($username, token());
}
}

And some test code:

$session = new Session('joe', '1234');
$session->mysql = 1234;
var_dump($session->mysql);
unset($session->mysql);
var_dump($session->mysql);

This is code of the added method:

function __unset($name) {
echo "Unsetting $name";
unset($this->storage->$name);
}

Check out the documentation to about the magic __unset method you need to add to your class:

http://php.net/manual/en/language.oop5.overloading.php#object.unset

__unset() is invoked when unset() is used on inaccessible properties.

How can I remove a private property from an array of object?

This is an array of Member object. A private attribute of an object can only be access through its method. You need to find the file that declares the class Member. Then add a public class method to do the unset. For example,

class Member {

// ...
public function unsetProjects()
{
unset($this->projects);
}

}

Then you should be able to do this:

foreach ($array as $value) {
$value->unsetProjects();
}


Related Topics



Leave a reply



Submit