Source Code:
(back to article)
<?php class User { public $username; public $email; public function __construct($username, $email) { $this->username = $username; $this->email = $email; } public function getUsername() { return $this->username; } public function getEmail() { return $this->email; } public function showInfo() { echo "Username: " . $this->username . "\n"; echo "Email: " . $this->email . "\n"; } } class Admin extends User { public $permissions; public function __construct($username, $email, $permissions) { parent::__construct($username, $email); $this->permissions = $permissions; } public function getPermissions() { return $this->permissions; } public function showInfo() { parent::showInfo(); echo "Permissions: " . $this->permissions . "\n"; } } $user = new User("John Doe", "john.doe@example.com"); $admin = new Admin("Jane Doe", "jane.doe@example.com", ["read", "write", "delete"]); $users = [$user, $admin]; foreach ($users as $user) { $user->showInfo(); }
Result:
Report an issue