get_class

(PHP 4, PHP 5)

get_class -- 返回对象的类名

描述

string get_class ( object obj )

返回对象实例 obj 所属类的名字。如果 obj 不是一个对象则返回 FALSE

注: get_class() 返回用户定义的类名的小写形式。在 PHP 扩展中定义的类则返回其原有的名字。

参见 get_parent_class()gettype()is_subclass_of()


add a note add a note User Contributed Notes
benjaminhill at gmail dot com
28-Apr-2006 01:47
More funkyness:

class Parent {
   function displayTableName() {
     echo get_class($this);
     echo get_class();
   }
}

class Child {
   function __construct() {
     $this->displayTableName();
   }
}

Will return
- Child
- Parent

So when they say "the object isn't required in PHP5" - they don't really mean it.
brjann at NOSPAMATALLgmail dot com
17-Nov-2005 04:39
This behavior is unexpected, but good to be aware of

class parentclass {
   public function getClass(){
       echo get_class($this); //using "$this"
   }
}
class child extends parentclass {
}

$obj = new child();
$obj->getClass(); //outputs "child"

class parentclass {
   public function getClass(){
       echo get_class(); //note, no "$this"
   }
}
class child extends parentclass {
}

$obj = new child();
$obj->getClass(); //outputs "parentclass"
davidc at php dot net
14-Oct-2005 01:25
As of php5, you cannot use get_class($this); in a public static function. You would have to do something like this:

<?php

class BooBoof {
  
public static function getclass()
   {
       return
__CLASS__;
   }
}

$c = BooBoof::getclass();
print
$c;
?>

To get the class since you cannot use
<?php
public
static function getclass()
{
   return
get_class($this);
}
?>

Rather simple and straightforward but that might help some people that are searching for it..
wired at evd dot ru
25-Sep-2005 05:25
There is one unexpected bahaviour (for me as least):

<?php

 
class parent
 
{
   ...

  
public function getInstance ($id)
   {
     ...

     print
get_class() . "\n" . __CLASS__;

     ...
   }
  }

  class
child extends parent
 
{
   ...
  }

 
child::getInstance(...);
?>

This code will produce:

  parent
  parent

So I can't make "new $className(...)" in getInstance(). The only option is to do a fabric.
kunxin at creaion dot com
25-Jul-2005 08:30
I just migrated from PHP 4 to PHP 5 and noticed that in PHP 5.03 that a lot of code dependent on get_class() and its variants stop working.

It turns out that get_class() and its variants are now case-sensitive.
refrozen dot com
06-Jul-2005 06:01
philip at cornado dot com, it returns the value of the class from which it was called, rather than the instance's name... causing inheritance to result in unexpected returns
MagicalTux at FF.ST
02-Feb-2004 08: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 02: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 11: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
21-Jun-2002 03:15
As of PHP 4.3.0 the constant __CLASS__ exists and contains the class name.