Heim  >  Artikel  >  Backend-Entwicklung  >  PHP get_class()函数的用法举例

PHP get_class()函数的用法举例

WBOY
WBOYOriginal
2016-07-25 08:57:421192Durchsuche
  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. //类内部调用
  8. $bar = new Foo();
  9. $bar->name_none_static();
  10. //类外部调用
  11. echo "Its name is " . get_class($bar) . "
    ";
  12. ?>
复制代码

输出结果: My name is Foo My name is Foo Its name is Foo

get_class 函数 (PHP 4, PHP 5)

get_class — 返回对象的类名

Report a bug reject note 说明

string get_class ([ object $obj ] ) 返回对象实例 obj 所属类的名字。如果 obj 不是一个对象则返回 FALSE。

Note: 在 PHP 扩展库中定义的类返回其原始定义的名字。在 PHP 4 中 get_class() 返回用户定义的类名的小写形式,但是在 PHP 5 中将返回类名定义时的名字,如同扩展库中的类名一样。

Note: 自 PHP 5 起,如果在对象的方法中调用则 obj 为可选项。

例1,使用 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. ?>
复制代码

输出: Its name is foo My name is foo



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn