class Person { public $name; public $age; public function __construct($name, $age) { $this->name = $name; $this->age = $age; } } $person = new Person("John", 25); echo $person->name; // Output: John echo $person->age; // Output: 25
class Connection { private $username; private $password; public function __construct($config) { $this->username = $config['username']; $this->password = $config['password']; } public function connect() { // Code to establish database connection using $this->username and $this->password } } $config = ['username' => 'root', 'password' => 'password']; $connection = new Connection($config); $connection->connect();In this example, the __construct() method takes a configuration array as an argument, and initializes private properties with the corresponding values from the array. The connect() method then uses these values to establish a database connection. Package library: This code doesn't specifically belong to any package library, as it is a core feature of PHP. However, it can be utilized in any custom class or third-party library.