Database

Class Functions in PHP

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.

[cc lang=”php”]
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();
[/cc]

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.

[cc lang=”php”]

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();

[/cc]

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.

[cc lang=”php”]

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”);

[/cc]

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.

[cc lang=”php”]

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”;
}
[/cc]

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.

[cc lang=”php”]
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”;
}

[/cc]

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.

Shares:
  • Giorgiolino
    November 11, 2011 at 7:19 pm

    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

    Reply
    • Avinash
      November 11, 2011 at 10:23 pm

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

      Reply
  • Giorgiolino
    December 20, 2019 at 2:52 pm

    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

    Reply
  • usama
    usama
    January 4, 2012 at 11:10 am

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

    Reply
    • Avinash
      January 4, 2012 at 9:35 pm

      Hi usama,

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

      Reply
  • usama
    usama
    December 20, 2019 at 2:54 pm

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

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *