Reader small image

You're reading from  Mastering PHP 7

Product typeBook
Published inJun 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781785882814
Edition1st Edition
Languages
Right arrow
Author (1)
Branko Ajzele
Branko Ajzele
author image
Branko Ajzele

Branko Ajzele is a respected and highly accomplished software developer, book author, solution specialist, consultant, and team leader. He currently works for Interactive Web Solutions Ltd (iWeb), where he holds the role of senior developer and is the director of iWeb's Croatia office. Branko holds several respected IT certifications, including Zend Certified PHP Engineer, Magento Certified Developer, Magento Certified Developer Plus, Magento Certified Solution Specialist, Magento 2 Certified Solution Specialist, Magento 2 Certified Professional Developer, to mention just a few. He was crowned the e-commerce Developer of the Year by the Digital Entrepreneur Awards in October 2014 for his excellent knowledge and expertise in e-commerce development.
Read more about Branko Ajzele

Right arrow

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:

<?php

class 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 unset() passing it the value at a given array key. We are trying to unset to the $age and $salary properties here. The $salary property does not really exist, neither as a class property nor as a data array key....

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Mastering PHP 7
Published in: Jun 2017Publisher: PacktISBN-13: 9781785882814

Author (1)

author image
Branko Ajzele

Branko Ajzele is a respected and highly accomplished software developer, book author, solution specialist, consultant, and team leader. He currently works for Interactive Web Solutions Ltd (iWeb), where he holds the role of senior developer and is the director of iWeb's Croatia office. Branko holds several respected IT certifications, including Zend Certified PHP Engineer, Magento Certified Developer, Magento Certified Developer Plus, Magento Certified Solution Specialist, Magento 2 Certified Solution Specialist, Magento 2 Certified Professional Developer, to mention just a few. He was crowned the e-commerce Developer of the Year by the Digital Entrepreneur Awards in October 2014 for his excellent knowledge and expertise in e-commerce development.
Read more about Branko Ajzele