• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP 戏法常量

php 搞代码 4年前 (2022-01-23) 15次浏览 已收录 0个评论

PHP 魔术常量

<?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  echo __LINE__;  //显示,__LINE__所在的行号  ?> 

?3,__CLASS__

<?php  class base_class  {   function say_a()   {   echo <b>/本文来源gao@!dai!ma.com搞$$代^@码5网@</b><strong>搞代gaodaima码</strong>"'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__

<?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名

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

?6,__NAMESPACE__

<?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"  ?>

?


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP 戏法常量
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址