PHP中的_FILE,CLASS等戲法變數

來源:互聯網
上載者:User
PHP中的__FILE,__CLASS等魔術變數

今天看到一個魔術變數,是以前沒見過的,__DIR__,我查了查,發現原來是php5.3新增的,順便舉幾個例子,解釋一下php的魔術變數

1,__FILE__

檔案的完整路徑和檔案名稱。如果用在被包含檔案中,則返回被包含的檔案名稱。自 PHP 4.0.2 起,__FILE__ 總是包含一個絕對路徑(如果是符號串連,則是解析後的絕對路徑),而在此之前的版本有時會包含一個相對路徑。
這個變數,我用的是最多的,估計也是大家用的最多的。

web伺服器都會指定一個documentroot的,但是不同的伺服器,設定的documentroot有可能是不同的,在這種情況下,把一個網站從一個伺服器搬家到另一個伺服器,這樣就有可能因為路徑的不同,造成網站跑不起來。

  1. /** ?
  2. 在你的公用的設定檔中,來設定你的根目錄,這樣就不用擔心經常搬家了。 ?
  3. */??
  4. define('ROOT_PATH',?dirname(__FILE__)?.?DIRECTORY_SEPARATOR); ??
  5. echo?ROOT_PATH; ??
  6. echo?"
    "; ??
  7. echo?__FILE__; ??
  8. echo?"
    "; ??
  9. echo?dirname(__FILE__); ??
  10. echo?"
    "; ??
  11. echo?dirname(dirname(__FILE__)); ??
  12. ?>??

";echo __FILE__;echo "
";echo dirname(__FILE__);echo "
";echo dirname(dirname(__FILE__));?>

2,__LINE__

檔案中的當前行號。這個變數在調試錯誤的時候,還是比較有作用的,其他的時候,沒什麼用處,純屬個人觀點。

  1. echo?__LINE__;??//顯示,__LINE__所在的行號 ??
  2. ?>??


3,__CLASS__

類的名稱,PHP5返回的結果是區分大小寫

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

"; } function say_b() { echo "'b' - said the " . get_class($this) . "
"; }}class derived_class extends base_class{ function say_a() { parent::say_a(); echo "'a' - said the " . __CLASS__ . "
"; } function say_b() { parent::say_b(); echo "'b' - said the " . get_class($this) . "
"; }}$obj_b = new derived_class();$obj_b->say_a();echo "
";$obj_b->say_b();?>結果為:'a' - said the base_class'a' - said the derived_class'b' - said the derived_class'b' - said the derived_class

有的時候,我們可以用get_class來代替__CLASS__

4,__FUNCTION__和__METHOD__

__FUNCTION__:函數名稱,php5中返回的結果是區分大小寫
__METHOD__:方法中的函數名稱,php5中返回的結果是區分大小寫

二個都是取得方法的名稱,有什麼不同呢?

  1. class?test ??
  2. { ??
  3. ?function?a() ??
  4. ?{ ??
  5. ?echo?__FUNCTION__; ??
  6. ?echo?"
    "; ??
  7. ?echo?__METHOD__; ??
  8. ?} ??
  9. } ??
  10. ??
  11. function?good?(){ ??
  12. ?echo?__FUNCTION__; ??
  13. ?echo?"
    "; ??
  14. ?echo?__METHOD__; ??
  15. } ??
  16. ??
  17. $test?=?new?test(); ??
  18. $test->a(); ??
  19. echo?"
    "; ??
  20. good(); ??
  21. ?> ??
  22. 返回結果: ??
  23. a ??
  24. test::a ??
  25. good ??
  26. good??

"; echo __METHOD__; }}function good (){ echo __FUNCTION__; echo "
"; echo __METHOD__;}$test = new test();$test->a();echo "
";good();?>返回結果:atest::agoodgood

相對於孤立的函數來說,二個都可以取出函數名,沒什麼區別,如果是class中的方法時,__FUNCTION__只能取出class的方法名,而__METHOD__不光能取出方法名,還能取出class名

5,__DIR__

檔案所在的目錄。如果用在被包括檔案中,則返回被包括的檔案所在的目錄。它等價於 dirname(__FILE__)。除非是根目錄,否則目錄中名不包括末尾的斜杠。(PHP 5.3.0中新增)

如果在5.3以前的版本中想用__DIR__的話,可以這樣

  1. if(!defined('__DIR__'))?{ ??
  2. ?$iPos?=?strrpos(__FILE__,?"/"); ??
  3. ?define("__DIR__",?substr(__FILE__,?0,?$iPos)?.?"/"); ??
  4. } ??
  5. ?>??


6,__NAMESPACE__

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

7,__STATIC__

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

  1. //php5.3 ??
  2. class?Model ??
  3. { ??
  4. ?public?static?function?find() ??
  5. ?{ ??
  6. ?echo?__STATIC__; ??
  7. ?} ??
  8. } ??
  9. ??
  10. class?Product?extends?Model?{} ??
  11. class?User?extends?Model?{} ??
  12. ??
  13. Product::find();?//?"Product" ??
  14. User::find();?//?"User" ??
  15. ?>??


?

原文出處:

http://blog.51yip.com/php/1165.html

  • 聯繫我們

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