PHP get_class 返回對象的類名

來源:互聯網
上載者:User

標籤:

get_class

(PHP 4, PHP 5)

get_class — 返回對象的類名

說明

string get_class ([ object $obj ] )

返回對象執行個體 obj 所屬類的名字。如果 obj 不是一個對象則返回 FALSE。

Note: 在 PHP 擴充庫中定義的類返回其原始定義的名字。在 PHP 4 中 get_class() 返回使用者定義的類名的小寫形式,但是在 PHP 5 中將返回類名定義時的名字,如同擴充庫中的類名一樣。
Note:

自 PHP 5 起,如果在對象的方法中調用則 obj 為可選項。

Example #1 使用 get_class()

<?phpclass foo{function foo(){// implements some logic}function name(){echo "My name is ", get_class($this), " ";}}// create an object$bar = new foo();// external callecho "Its name is ", get_class($bar), " ";// internal call$bar->name();

以上常式會輸出:

Its name is foo
My name is foo

 

 

另外,PHP 的魔術常量 __CLASS__ ,也可表示當前類,不過它在方法內才真正起作用,如下所示:

<?phpclass jianbao{public function __construct(){echo "My class name is " . get_class($this), "<br/>";echo ‘__CLASS__:‘, __CLASS__, "<br/>";}}$jianbao = new jianbao();echo "It is " . get_class($jianbao);

輸出:

My class name is jianbao__CLASS__:jianbaoIt is jianbao

PHP get_class 返回對象的類名

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.