Stack Overflow
Stack Overflow

Reputation: 2968

Why we use ArrayAccess::offsetUnset(), instead we can use unset() in php?

Why we use ArrayAccess::offsetUnset() instead i hope unset() is enough to use. But php.net stated that:

Note: This method will not be called when type-casting to (unset)

Can anyone tell how we use it, whether it automatically unset the called offset element from the class that implements ArrayAccess interface ?

Reference link http://php.net/manual/en/arrayaccess.offsetunset.php.

Thanks !!!

Upvotes: 2

Views: 1188

Answers (1)

Progman
Progman

Reputation: 19555

The offsetUnset() method is called, when the array access expression is used in the unset() function like this:

unset($yourSpecialObject['abc']);

However it is not called in the following statements:

$yourSpecialObject['abc'] = null;         // offsetSet() is called instead
$yourSpecialObject['abc'] = (unset)'abc'; // offsetSet() is called instead
(unset)$yourSpecialObject['abc'];         // offsetGet() is called instead

Upvotes: 6

Related Questions