-1

I’m just learning OOP Php, I understand the basic principles of OOP programming, and trying to do exercises. I wanna do a simple messaging application, where users can message each other. Like email, but in a closed system.

My idea is when an user logs in, i create an user object that contains the function that an user can do and if an admin logs in i create an admin object that extends the user class with added admin functions.

My question is Can I check in other class if the object is an admin or an user class without injecting in the classes or create the objects inside the checking class. Like this:

  class Checker{
      // some variables
      public function isAdmin($object ){
                if($object istanceof Admin){
                    return 1;
                 } else{
                    return 0;
                 }
           }
      }
Ash-b
  • 705
  • 7
  • 11
  • yes, if you correct two typos: `istanceof` -> `instanceof`, and two missing `;` after `return 1;` – Jeff May 18 '18 at 09:20

1 Answers1

0

IMO using reflection and introspection for non-debug purposes is an anti-practice. PHP 5 Reflection API performance

Do it like this:

class User implements SystemUserInterface
{
    public function isAdmin()
    {
        return false;
    }
}

class Admin extends User
{
    public function isAdmin()
    {
         return true;
    }
    // other stuff that only admin can do..
}

function checkUser($user)
{
   echo "You have created ".($user->isAdmin()? "admin":"regular")." user";
}
Eriks Klotins
  • 4,042
  • 1
  • 12
  • 26