phparrayaccess

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 !!!


Solution

  • 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