name = $name; } public function getName() { return $this->name; } // Permission Methods public static function hasReadPermission() { return true; } public static function hasModifyPermission() { return false; } public static function hasDeletePermission(){ return false; } // Customization methods public static function wantsFlashInterface() { return true; } protected $name = NULL; } class GuestUser extends User{ public static function hasModifyPermission() { return true; } } class AdminUser extends User{ public static function hasModifyPermission() { return true; } public static function hasDeletePermission() { return true; } public static function wantsFlashInterface() { return false; } } Class UserFactory{ private static $users = array("xenoix" => "admin", "reZo" => "guest", "ace" => "customer"); public static function Create($name) { if(!isset(self::$users[$name])){ // Error out user doesnt exist echo 'Error: User does not exist'; } switch(self::$users[$name]){ case 'guest': return new GuestUser($name); break; case 'customer': return new CustomerUser($name); break; case 'admin': return new AdminUser($name); break; default: // By default, return a guest user return new GuestUser($name); break; } } } function boolToStr($b) { $b = ($b == true) ? "Yes\n" : "No\n"; return $b; } function displayPermissions(User $obj) { echo $obj->getName() . "'s permissions:\n"; echo "
Read: " . boolToStr($obj->hasReadPermission()); echo "
Modify: " . boolToStr($obj->hasModifyPermission()); echo "
Delete: " . boolToStr($obj->hasDeletePermission()); } function displayRequirements(){ if($obj->wantsFlashInterface()) { echo $obj->getName() . ' requires Flash
'; } } $logins = array('xenoix','reZo','ace'); foreach($logins as $login){ displayPermissions(UserFactory::Create($login)); displayRequirements(UserFactory::Create($login)); } ?>