class Person { private $name; public function __construct($name) { $this->name = $name; } public function getName() { return $this->name; } } $person = new Person('John Doe'); $name = $person->getAttribute('name');
class Person { private $name; public function __construct($name) { $this->name = $name; } public function __get($attribute) { return $this->$attribute; } } $person = new Person('John Doe'); $name = $person->getAttribute('name');In this example, we create a Person class with a private $name property. We then create a __get magic method that allows us to retrieve the value of any attribute with a dynamic name. We then use the getAttribute method to retrieve the value of the $name property. Package library: The getAttribute method is part of the PHP core language, so it does not require any specific package or library.