Class Functions in PHP

  • Avinash
  • 4
  • Nov 10, 2011
  • Tutorial

Now in this post I am going to explain various PHP functions which will help us to get the information about the class. We can use this function in class or with the object of the class.

From the term “information about the class“, I mean about the name of the class, name of the patent class, gettting the parent class, etc.

Here I am going to explain below functions:

  • get_class()
  • get_called_class()
  • get_parent_class()
  • is_subclass_of()
  • is_a()

Let’s dive into each one:

get_class()

This function returns the name of the class of given object. We have to pass the object as a parameter. This function return FALSE if passed parameter is not an object.

This parameter will be ommited if you call this function from the class.

Note: If you pass other than object it will throw E_WARNING level warning.

class first_class { function first_method() { echo "My Name is : ".get_class($this); } } $obj= new first_class(); // Output: Name: first_class echo "Name: ". get_class($obj); // "My Name is : first_class; echo obj->first_method();
  class first_class
  {
    function first_method()
    {
      echo "My Name is : ".get_class($this);
    }
  }
 
  $obj= new first_class();
 
  // Output: Name: first_class
  echo "Name: ". get_class($obj);
 
  // "My Name is : first_class;
  echo obj->first_method();

get_called_class()

This function will returns the name of the class the static method is called in. This function will return the class name and returns FALSE if its called from outside of the class.

class first_class { static public function first_function() { echo get_called_class(); } } class second_class extends first_class { } // Output: first_class first_class::first_function(); // Output: second_class second_class::first_function();
  class first_class
  {
    static public function first_function()
    {
      echo get_called_class();
    }
  }

  class second_class extends first_class
  {
 
  }

  // Output: first_class
  first_class::first_function();
 
  // Output: second_class
  second_class::first_function();

get_parent_class()

This function returns the name of the class. We need to pass either object of a class or a class name itself.

If Parent class not found then it will return FALSE. Also if this function called outside the class without parameter will return FALSE.

class first_class { function parents() { /* Code Here */ } } class second_class extends first_class { function childs() { echo "My parent is: ". get_parent_class($this); } } $obj = new second_class(); // Output: My parent is: first_class $obj->childs(); // Output: My parent is: first_class $obj->childs("second_class");
  class first_class
  {
    function parents()
    {
      /* Code Here */
    }
  }
 
  class second_class extends first_class
  {
    function childs()
    {
       echo "My parent is: ". get_parent_class($this);
    }
  }
 
  $obj = new second_class();
 
  // Output: My parent is: first_class
  $obj->childs();
 
  // Output: My parent is: first_class
  $obj->childs("second_class");

is_subclass_of()

This function takes two parameters. First one is either name or object of an class and second parameter will be a name of an object.

So This function will returns TRUEis class in second parameter is parent class of object or class in second parameter.

And example will make you more clear on this.

class parent_class { } class child_class extends parent_class { } $child = new child_class(); if(is_subclass_of($child, "parent_class")) { echo "child_class is a subsclass of parent_class"; } else { echo "Wrong Selection"; }
  class parent_class
  {
   
  }
 
  class child_class extends parent_class
  {
 
  }
 
  $child = new child_class();
 
  if(is_subclass_of($child, "parent_class"))
  {
    echo "child_class is a subsclass of parent_class";
  }
  else
  {
    echo "Wrong Selection";
  }

is_a()

This function tales two parameter. First one is the object of the class and second one is the name of the class.

This function checks if given object is an object of given class or not. If it is then it will return TRUE other wise FALSE.

class first_class { } $obj = new first_class(); if(is_a($obj,"first_class")) { echo "$obj is Object of the class first_class"; } else { echo "Wrong Selection"; }
  class first_class
  {
   
  }
 
  $obj = new first_class();
 
  if(is_a($obj,"first_class"))
  {
    echo "$obj is Object of the class first_class";
  }
  else
  {
    echo "Wrong Selection";
  }

Coclusion:

Finally you can get the basic information about the class, parent class using the name or the object of the class. It would be great if you share your comments about how you are using these methods in you work.

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by:

  • http://about.me/georges.njockbot Giorgiolino

    Hello Avinash,

    nice article with all the facts carefully explained.
    Since the point here is “to get the information about the class”, wouldn’t it be better to use the Reflection API which suits best for that matter ?

    Personnally, i happen to use the magic constants __CLASS__ and __METHOD__ when needed here and there. But if the point is getting more thorough infos about objects and/or classes, i’d rather use Reflection API.

    How about you ? Did you know about Reflection API ?

    Regards

    • http://www.xpertdeveloper.com Avinash

      I have not explored it yet.. But once that done, I will surely post about it… :)

  • usama

    Hi Avinash, I have a doubt, can you clear it.

    • http://www.xpertdeveloper.com Avinash

      Hi usama,

      Comments are open for all, you can ask without asking..