Using __set()
Aside from method overloading, property overloading is another aspect of the PHP overloading capabilities. There are four magic methods in PHP that support the property overloading: __set()
, __get()
, __isset()
, and __unset()
. Throughout this section, we will take a closer look at the __set()
method.
The __set()
magic method is triggered when trying to write data to inaccessible properties.Â
The method accepts two parameters, as per the following synopsis:
public void __set(string $name, mixed $value)
Whereas, the __set()
 method parameters have the following meaning:
$name
: This is the name of the property being interacted with$value
: This is the value that the$name
property should be set to
Let's take a look at the following object context example:
<?php class User { private $data = array(); private $name; protected $age; public $salary; public function __set($name, $value) { $this->data[$name] = $value; } } $user = new User(); $user->name = 'John'...