Skip to Content
Mastering PHP 7
book

Mastering PHP 7

by Branko Ajzele
June 2017
Intermediate to advanced
536 pages
9h 49m
English
Packt Publishing
Content preview from Mastering PHP 7

Using __unset()

The __unset() magic method is triggered by calling the unset() language constructs on inaccessible properties. The method accepts a single parameter, as per the following synopsis:

public bool __unset(string $name)

The $name argument is the name of the property being interacted with.

Let's take a look at the following object context example:

<?phpclass User{  private $data = [    'name' => 'John',    'age' => 34,  ];  public function __unset($name)  {    unset($this->data[$name]);  }}$user = new User();var_dump($user);unset($user->age);unset($user->salary);var_dump($user);

The User class declares a single private $data array property, alongside the __unset() magic method. The method itself is quite simple; it merely calls for the 

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning PHP 7

Learning PHP 7

Antonio L Zapata (GBP)
Upgrading to PHP 5

Upgrading to PHP 5

Adam Trachtenberg

Publisher Resources

ISBN: 9781785882814Supplemental Content