Solution Review: Abstract Classes and Methods
Let's look at the solution of the Abstract Classes and Methods challenge.
We'll cover the following...
Solution
Press + to interact
<?phpabstract class User {protected $username = '';abstract public function stateYourRole();public function setUsername($name) {$this -> username = $name;}public function getUsername() {return $this -> username;}}class Admin extends User {public function stateYourRole() {return "admin";}}class Viewer extends User {public function stateYourRole() {return strtolower(__CLASS__);}}function test(){$admin1 = new Admin();$admin1 -> setUsername("Balthazar");return $admin1 -> stateYourRole();}echo test();?>
Explanation
-
Line 2: We create a
User
abstract class with a protected$username
property. -
Line 5: We add an abstract
stateYourRole()
method in theUser
class. ...