class Person { private $name; private $age; public function __construct($attributes) { self::setAttributes($attributes); } public function setAttributes($attributes) { foreach ($attributes as $key => $value) { if (property_exists($this, $key)) { $this->$key = $value; } } } } $attributes = array('name' => 'John', 'age' => 25); $person = new Person($attributes); echo $person->name . ' is ' . $person->age . ' years old.';
namespace Example; class User { public $username; private $password; public function __construct($username, $password) { $this->username = $username; $this->password = $password; } public function setAttributes($attributes) { self::username = $attributes['username']; self::password = $attributes['password']; } } $user = new User('john', 'password1'); $userAttributes = array('username' => 'doe', 'password' => 'password2'); $user->setAttributes($userAttributes); echo $user->username; // Output: doeIn this example, the User class has a setAttributes method that sets the username and password attributes using the self keyword. The User object's attributes can be updated by calling the setAttributes method. Package library: This feature is built into the PHP language and does not require a package or library.