php __FILE__,__CLASS__等魔術變量,及實例

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

1,__FILE__

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

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

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

2,__LINE__

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

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

3,__CLASS__

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

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

class derived_class extends base_class
{
 function say_a()
 {
 parent::say_a();
 echo "'a' - said the " . __CLASS__ . "<br/>";
 }
 function say_b()
 {
 parent::say_b();
 echo "'b' - said the " . get_class($this) . "<br/>";
 }
}

$obj_b = new derived_class();
$obj_b->say_a();
echo "<br/>";
$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. <?php   
  2. class test   
  3. {   
  4.  function a()   
  5.  {   
  6.  echo __FUNCTION__;   
  7.  echo "<br>";   
  8.  echo __METHOD__;   
  9.  }   
  10. }   
  11.   
  12. function good (){   
  13.  echo __FUNCTION__;   
  14.  echo "<br>";   
  15.  echo __METHOD__;   
  16. }   
  17.   
  18. $test = new test();   
  19. $test->a();   
  20. echo "<br>";   
  21. good();   
  22. ?>   
  23. 返回結果:   
  24. a   
  25. test::a   
  26. good   
  27. good  
<?php
class test
{
 function a()
 {
 echo __FUNCTION__;
 echo "<br>";
 echo __METHOD__;
 }
}

function good (){
 echo __FUNCTION__;
 echo "<br>";
 echo __METHOD__;
}

$test = new test();
$test->a();
echo "<br>";
good();
?>
返回結果:
a
test::a
good
good

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

5,__DIR__

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

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

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

6,__NAMESPACE__

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

7,__STATIC__

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

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

class Product extends Model {}
class User extends Model {}

Product::find(); // "Product"
User::find(); // "User"
?>
轉載請註明
作者:海底蒼鷹
地址:http://blog.51yip.com/php/1165.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章