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