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; // Outputs: John echo $person->age; // Outputs: 25
class Database { private $host; private $username; private $password; private $dbname; private $conn; public function __construct($host, $username, $password, $dbname) { $this->host = $host; $this->username = $username; $this->password = $password; $this->dbname = $dbname; $this->conn = new mysqli($this->host, $this->username, $this->password, $this->dbname); if ($this->conn->connect_error) { die("Connection failed: " . $this->conn->connect_error); } } public function getConnection() { return $this->conn; } } // Create a new Database object $db = new Database('localhost', 'username', 'password', 'dbname'); // Get the database connection $conn = $db->getConnection();In this example, we have a `Database` class that creates a new mysqli database connection in the `__construct` method. If the connection fails, we use the `die` function to stop the script and display an error message. We then have a public method `getConnection` that returns the database connection object. The `Database` class in this example is not part of a package library, as it is a stand-alone class. However, it could be included as part of a larger package or library.