PHP魔術變數總結

來源:互聯網
上載者:User

標籤:函數   rpo   作用   amp   code   class   com   區分   nts   

php手冊裡面的解釋

__FUNCTION__   returns only the name of the function   返回的僅僅是函數的名稱

__METHOD__   returns the name of the class alongwith the name of the function 返回的是函數的名稱,如果取出class的方法名,而__METHOD__不光能取出方法名,還能取出class名

 1 class trick 2 { 3       function doit() 4       { 5                 echo __FUNCTION__; 6       } 7       function doitagain() 8       { 9                 echo __METHOD__;10       }11 }12 $obj=new trick();13 $obj->doit();14 //output will be ----  doit  輸出doit  15 $obj->doitagain();16 //output will be ----- trick::doitagain 輸出 trick::doitagain

 

__CLASS__  類的名稱

get_class()    返回對象的類名   string get_class ([ object $obj ] ) 返回對象執行個體 obj 所屬類的名字。如果 obj 不是一個對象則返回 FALSE

The __CLASS__ magic constant nicely complements the get_class() function.  魔術常量__CLASS__ 很好的補充了get_class()函數

Sometimes you need to know both:    有時候你會知道下面兩個
- name of the inherited class              繼承類的class  名稱
- name of the class actually executed      實際執行的class 的名稱

Here‘s an example that shows the possible solution:  下面是執行個體

 1 <?php 2  3 class base_class 4 { 5     function say_a() 6     { 7         echo "‘a‘ - said the " . __CLASS__ . "<br/>"; 8     } 9 10     function say_b()11     {12         echo "‘b‘ - said the " . get_class($this) . "<br/>";13     }14 15 }16 17 class derived_class extends base_class18 {19     function say_a()20     {21         parent::say_a();22         echo "‘a‘ - said the " . __CLASS__ . "<br/>";23     }24 25     function say_b()26     {27         parent::say_b();28         echo "‘b‘ - said the " . get_class($this) . "<br/>";29     }30 }31 32 $obj_b = new derived_class();33 34 $obj_b->say_a();35 echo "<br/>";36 $obj_b->say_b();37 38 ?>

輸出的內容:
‘a‘ - said the base_class
‘a‘ - said the derived_class

‘b‘ - said the derived_class
‘b‘ - said the derived_class

 

__DIR__檔案所在的目錄。如果用在被包括檔案中,則返回被包括的檔案所在的目錄。它等價於 dirname(__FILE__)  (PHP 5.3.0中新增)

__FILE__  檔案的完整路徑和檔案名稱。如果用在被包含檔案中,則返回被包含的檔案名稱

如果你想使用在未定義__DIR__  使用可以用如下方法:

<?php//第一種方法:if(!defined(‘__DIR__’)) {$iPos = strrpos(__FILE__, “/”);define(“__DIR__”, substr(__FILE__, 0, $iPos) . “/”);}//第二種方法:function abspath($file){  return realpath(dirname($file));}abspath(__FILE__);?>

 

__NAMESPACE__  當前命名空間的名稱(大小寫敏感)。這個常量是在編譯時間定義的(PHP 5.3.0 新增)

__STATIC__  當你調用class的靜態方法時,返回class名稱,區分大小寫。如果在繼承中調用的話,不管在繼承中有沒有定義,都能返回繼承的class名。

__LINE__  檔案中的當前行號。

__TRAIT__    Trait 的名字(PHP 5.4.0 新加)。自 PHP 5.4 起此常量返回 trait 被定義時的名字(區分大小寫)。Trait 名包括其被聲明的作用地區(例如 Foo\Bar)。 

 

PHP魔術變數總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.