__get method in php
In PHP, the "__get" method is a magic method that is called when an undefined or inaccessible property is accessed using the object's arrow operator "->".
Here is an example of how the "__get" method can be defined and used:
class MyClass {
private $data = array('name' => 'John', 'age' => 30);
public function __get($property) {
if (isset($this->data[$property])) {
return $this->data[$property];
} else {
return null;
}
}
}
$obj = new MyClass();
echo $obj->name; // Output: John
echo $obj->age; // Output: 30
echo $obj->address; // Output: null
In the above example, when the "name" and "age" properties are accessed using the arrow operator, the "__get" method is called and returns the corresponding value from the "data" array. If an undefined property, such as "address", is accessed, the "__get" method returns null.
Note that the "__get" method can only be defined within a class definition and cannot be called directly from outside the class.