class Person { private $name; private $age; public function __construct($name, $age) { $this->name = $name; $this->age = $age; } public function getInfo() { return "Name: {$this->name}, Age: {$this->age}"; } } $jane = new Person("Jane", 30); echo $jane->getInfo(); // Output: Name: Jane, Age: 30
class User { private $username; private $email; public function __construct($username, $email) { if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { throw new InvalidArgumentException("Invalid email format"); } $this->username = $username; $this->email = $email; } public function getInfo() { return "Username: {$this->username}, Email: {$this->email}"; } } try { $user = new User("johndoe123", "johndoe@example.com"); echo $user->getInfo(); } catch (InvalidArgumentException $e) { echo "Error: {$e->getMessage()}"; }In the above example, we have defined a User class with two properties: username and email. In the constructor, we validate the email using the filter_var function with FILTER_VALIDATE_EMAIL flag. If the email is not valid, we throw an exception with a message "Invalid email format". In the calling code, we try to create a new User object with valid email. If the email is not valid, we catch the exception and display the error message. Based on the code examples, it is difficult to determine a specific package or library that is being used, as the code is written using core PHP functionality. However, the code does demonstrate basic object-oriented programming principles and error handling techniques.