Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: get_class - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net

search for in the

get_declared_classes" width="11" height="7"/> <get_class_vars
Last updated: Sun, 19 Nov 2006
view this page in

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.

Note: Since PHP 5, obj is optional if called from the object's method.

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



add a note add a note User Contributed Notes
get_class
luke at liveoakinteractive dot com
07-Dec-2006 12:03
This note is a response to the earlier post by davidc at php dot net. Unfortunately, the solution posted for getting the class name from a static method does not work with inherited classes.

Observe the following:
<?php
class BooBoof {
 
public static function getclass() {
   return
__CLASS__;
  }

 
public function retrieve_class() {
   return
get_class($this);
  }
}

class
CooCoof extends BooBoof {
}

echo
CooCoof::getclass();
// outputs BooBoof

$coocoof = new CooCoof;
echo
$coocoof->retrieve_class();
// outputs CooCoof
?>

__CLASS__ and get_class($this) do not work the same way with inherited classes. I have been thus far unable to determine a reliable way to get the actual class from a static method.
benjaminhill at gmail dot com
28-Apr-2006 02: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 05: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 02: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 06: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 09: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 07: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 09: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 03: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
14-Aug-2002 12: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 04:15
As of PHP 4.3.0 the constant __CLASS__ exists and contains the class name.

 
show source | credits | sitemap | contact | advertising | mirror sites