PHP get_class() function

In this chapter you will learn:

  1. What is PHP get_class() function
  2. Syntax for PHP get_class() function
  3. Parameters for PHP get_class() function
  4. Return Values
  5. Example - Get the class name

Description

get_class() returns the class name of the object you pass to it.

Syntax

string get_class ([ object $object = NULL ] )

Parameters

  • object - The tested object. This parameter may be omitted when inside a class.

Return Values

Returns the name of the class of which object is an instance. Returns FALSE if object is not an object.

If object is omitted when inside a class, the name of that class is returned.

Example

Get the class name


<?php/*from j  a  v a  2s .com*/

class foo {
    function name()
    {
        echo "My name is " , get_class($this) , "\n";
    }
}

$bar = new foo();
echo "Its name is " , get_class($bar) , "\n";
$bar->name();

?>

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. When to use PHP get_declared_classes() function
  2. Syntax for PHP get_declared_classes() function
  3. Return Values
  4. Example - get the declared classes
Home » PHP Tutorial » PHP Class Functions
PHP class_exists() function
PHP get_class() function
PHP get_declared_classes() function
PHP get_declared_interfaces() function
PHP get_defined_functions() function
PHP instanceof
PHP serialize() function