php __FILE__,__CLASS__等魔术变量,及实例

今天看到一个魔术变量,是以前没见过的,__DIR__,我查了查,发现原来是php5.3新增的,顺便举几个例子,解释一下php的魔术变量

1,__FILE__

文件的完整路径和文件名。如果用在被包含文件中,则返回被包含的文件名。自 PHP 4.0.2 起,__FILE__ 总是包含一个绝对路径(如果是符号连接,则是解析后的绝对路径),而在此之前的版本有时会包含一个相对路径。
这个变量,我用的是最多的,估计也是大家用的最多的。

web服务器都会指定一个documentroot的,但是不同的服务器,设置的documentroot有可能是不同的,在这种情况下,把一个网站从一个服务器搬家到另一个服务器,这样就有可能因为路径的不同,造成网站跑不起来。

<?php   
  1. /**  
  2. 在你的公用的配置文件中,来设置你的根目录,这样就不用担心经常搬家了。  
  3. */  
  4. define('ROOT_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);   
  5. echo ROOT_PATH;   
  6. echo "<br>";   
  7. echo __FILE__;   
  8. echo "<br>";   
  9. echo dirname(__FILE__);   
  10. echo "<br>";   
  11. echo dirname(dirname(__FILE__));   
  12. ?>  
<?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__

文件中的当前行号。这个变量在调试错误的时候,还是比较有作用的,其他的时候,没什么用处,纯属个人观点。

<?php   
  1. echo __LINE__;  //显示,__LINE__所在的行号   
  2. ?>  
<?php
echo __LINE__;  //显示,__LINE__所在的行号
?>

3,__CLASS__

类的名称,PHP5返回的结果是区分大小写的

<?php   
  1. class base_class   
  2. {   
  3.  function say_a()   
  4.  {   
  5.  echo "'a' - said the " . __CLASS__ . "<br/>";   
  6.  }   
  7.  function say_b()   
  8.  {   
  9.  echo "'b' - said the " . get_class($this) . "<br/>";   
  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__ . "<br/>";   
  19.  }   
  20.  function say_b()   
  21.  {   
  22.  parent::say_b();   
  23.  echo "'b' - said the " . get_class($this) . "<br/>";   
  24.  }   
  25. }   
  26.   
  27. $obj_b = new derived_class();   
  28. $obj_b->say_a();   
  29. echo "<br/>";   
  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  
<?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中返回的结果是区分大小写的

二个都是取得方法的名称,有什么不同呢?

<?php   
  1. class test   
  2. {   
  3.  function a()   
  4.  {   
  5.  echo __FUNCTION__;   
  6.  echo "<br>";   
  7.  echo __METHOD__;   
  8.  }   
  9. }   
  10.   
  11. function good (){   
  12.  echo __FUNCTION__;   
  13.  echo "<br>";   
  14.  echo __METHOD__;   
  15. }   
  16.   
  17. $test = new test();   
  18. $test->a();   
  19. echo "<br>";   
  20. good();   
  21. ?>   
  22. 返回结果:   
  23. a   
  24. test::a   
  25. good   
  26. 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__的话,可以这样

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

6,__NAMESPACE__

当前命名空间的名称(大小写敏感)。这个常量是在编译时定义的(PHP 5.3.0 新增)

7,__STATIC__

当你调用class的静态方法时,返回class名称,区分大小写。如果在继承中调用的话,不管在继承中有没有定义,都能返回继承的class名。

<?php   
  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. ?>  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值