Call overrided methods in PHP

Description

The following code shows how to call overrided methods.

Example


/*from w  w w  .  ja va 2  s .  c o  m*/
<?php
  class User
  {
    protected function isAuthorized()
    {
      return(FALSE);
    }

    public function getName()
    {
      return($this->name);
    }

    public function deleteUser($username)
    {
      if(!$this->isAuthorized())
      {
        print("You are not authorized.<br>\n");
        return(FALSE);
      }

      //delete the user
      print("User deleted.<br>\n");
    }
  }

  class AuthorizedUser extends User
  {
    protected function isAuthorized()
    {
      return(TRUE);
    }
  }

  $user = new User;
  $admin = new AuthorizedUser;

  //not authorized
  $user->deleteUser("Zeev");

  //authorized
  $admin->deleteUser("Zeev");
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Language Basic »




PHP Introduction
PHP Operators
PHP Statements
Variable
PHP Function Create
Exception
PHP Class Definition