search for in the  
<get_class_varsget_declared_classes>
Last updated: Thu, 19 May 2005

get_class

(PHP 4, PHP 5)

get_class -- Returns the name of the class of an object

Description

string get_class ( object obj )

This function returns the name of the class of which the object obj is an instance. Returns FALSE if obj is not an object.

Note: A class defined in a PHP extension is returned in its original notation. In PHP 4 get_class() returns a user defined class name in lowercase, but in PHP 5 it will return the class name in it's original notation too, just like class names from PHP extensions.

Example 1. Using get_class()

<?php

class foo {
   function
foo()
   {
  
// implements some logic
  
}

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

// create an object
$bar = new foo();

// external call
echo "Its name is " , get_class($bar) , "\n";

// internal call
$bar->name();

?>

The above example will output:

Its name is foo
My name is foo

See also get_parent_class(), gettype(), and is_subclass_of().



User Contributed Notes
get_class
MagicalTux at FF.ST
02-Feb-2004 06:11
Note that the constant __CLASS__ is different from get_class($this) :
<?
 
class test {
   function
whoami() {
     echo
"Hello, I'm whoami 1 !\r\n";
     echo
"Value of __CLASS__ : ".__CLASS__."\r\n";
     echo
"Value of get_class() : ".get_class($this)."\r\n\r\n";
   }
  }
  class
test2 extends test {
   function
whoami2() {
     echo
"Hello, I'm whoami 2 !\r\n";
     echo
"Value of __CLASS__ : ".__CLASS__."\r\n";
     echo
"Value of get_class() : ".get_class($this)."\r\n\r\n";
    
parent::whoami(); // call parent whoami() function
  
}
  }
 
$test=new test;
 
$test->whoami();
 
$test2=new test2;
 
$test2->whoami();
 
$test2->whoami2();
?>

The output is :
Hello, I'm whoami 1 !
Value of __CLASS__ : test
Value of get_class() : test

Hello, I'm whoami 1 !
Value of __CLASS__ : test
Value of get_class() : test2

Hello, I'm whoami 2 !
Value of __CLASS__ : test2
Value of get_class() : test2

Hello, I'm whoami 1 !
Value of __CLASS__ : test
Value of get_class() : test2

In fact, __CLASS__ returns the name of the class the function is in and get_class($this) returns the name of the class which was created.
Dan
30-Jan-2003 12:00
This function does return the class name in lowercase, but that does not seem to make any difference. The code below, although very sloppy, works fine in all of the following configurations.

PHP 4.2.2 on Windows NT5 with Apache 1.3.24
PHP 4.2.1 in Zend Development Environment on box above
PHP 4.2.3 on Linux RedHat 7.3 with Apache 1.3.27

class TeSt {
   var $a;
   var $b = "Fred";

   // Notice the case difference in the constructor name

   function Test() {
     $classname = get_class($this); // $classname = "test"
     $this->ra = get_class_vars($classname);
   }
}
// Next line also works with Test(), TEST(), or test()
$obj = new TeSt();
print_r($obj->ra);

Result :
   Array
   (
       [a] =>
       [b] => Fred
   )
oliver DOT pliquett @mediagear DOT de
13-Aug-2002 10:07
This function can become _VERY_ helpful if you want to return a new object of the same type. See this example:

<?php
class Foo{
   var
$name;
  
   function
Foo( $parameter ){
      
$this->name = $parameter;
   }

   function
whoami() {
       echo
"I'm a " . get_class( $this ) ."\n";
   }
  
   function
getNew() {
      
$className = get_class( $this );
      
      
// here it happens:
      
return new $className ( "world" ) ;
   }
}
class
Bar extends Foo {

   function
Bar( $name ){
      
$this->Foo( $name );
   }

   function
welcome() {
       echo
"Hello, " . $this->name "! \n";
   }
}

// We generate a Bar object:
$myBar = new Bar( "Oliver" );
$myBar->welcome();

//now let's instanciate a new Bar object.
//note: this method is inherited from Foo by Bar!

$baba = $myBar->getNew();

$baba->welcome();
$baba->whoami();

/* Output:
Hello, Oliver!
Hello, world!
I'm a bar
*/
?>
philip at cornado dot com
20-Jun-2002 02:15
As of PHP 4.3.0 the constant __CLASS__ exists and contains the class name.

<get_class_varsget_declared_classes>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT