Home > Backend Development > PHP Tutorial > Usage examples of PHP get_class() function

Usage examples of PHP get_class() function

WBOY
Release: 2016-07-25 08:57:42
Original
1257 people have browsed it
  1. class Foo {
  2. function name_none_static(){
  3. echo "My name is " . get_class() . "
    ";
  4. echo "My name is " . get_class($ this) . "
    ";
  5. }
  6. }
  7. //Call inside the class
  8. $bar = new Foo();
  9. $bar->name_none_static();
  10. //Call outside the class
  11. echo " Its name is " . get_class($bar) . "
    ";
  12. ?>
Copy code

Output result: My name is Foo My name is Foo Its name is Foo

get_class function (PHP 4, PHP 5)

get_class — Returns the class name of the object

Report a bug reject note description

string get_class ([ object $obj ] ) Returns the name of the class to which the object instance obj belongs. Returns FALSE if obj is not an object.

Note: Classes defined in PHP extension libraries return the name of their original definition. In PHP 4 get_class() returned the lowercase form of the user-defined class name, but in PHP 5 it will return the name of the class name as it was defined, just like the class name in the extension library.

Note: Since PHP 5, obj is optional if called within a method of an object.

Example 1, using get_class()

  1. class foo {

  2. function foo()
  3. {
  4. // implements some logic
  5. }

  6. function name()

  7. {
  8. echo "My name is " , get_class($this) , "n";
  9. }
  10. }

  11. // create an object

  12. $bar = new foo();
  13. // external call

  14. echo "Its name is " , get_class($bar) , "n";

  15. // internal call

  16. $bar->name( );
  17. ?>

Copy the code

output: Its name is foo My name is foo



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template