PHP中的__FILE,__CLASS等魔术变量
今天看到一个魔术变量,是以前没见过的,__DIR__,我查了查,发现原来是php5.3新增的,顺便举几个例子,解释一下php的魔术变量1,__FILE__
文件的完整路径和文件名。如果用在被包含文件中,则返回被包含的文件名。自 PHP 4.0.2 起,__FILE__ 总是包含一个绝对路径(如果是符号连接,则是解析后的绝对路径),而在此之前的版本有时会包含一个相对路径。
这个变量,我用的是最多的,估计也是大家用的最多的。
web服务器都会指定一个documentroot的,但是不同的服务器,设置的documentroot有可能是不同的,在这种情况下,把一个网站从一个服务器搬家到另一个服务器,这样就有可能因为路径的不同,造成网站跑不起来。
[*]<?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__));
[*]?>
<?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__所在的行号
[*]?>
<?php
echo __LINE__;//显示,__LINE__所在的行号
?>
3,__CLASS__
类的名称,PHP5返回的结果是区分大小写的
[*]<?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
<?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 thederived_class
'b' - said the derived_class
有的时候,我们可以用get_class来代替__CLASS__
4,__FUNCTION__和__METHOD__
__FUNCTION__:函数名称,php5中返回的结果是区分大小写的
__METHOD__:方法中的函数名称,php5中返回的结果是区分大小写的
二个都是取得方法的名称,有什么不同呢?
[*]<?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
<?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
[*]
if(!defined('__DIR__')) {
[*]
$iPos = strrpos(__FILE__, "/");
[*]
define("__DIR__", substr(__FILE__, 0, $iPos) . "/");
[*]}
[*]?>
<?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
[*]
//php5.3
[*]
class Model
[*]{
[*]
public static function find()
[*] {
[*]
echo __STATIC__;
[*] }
[*]}
[*]
[*]
class Product extends Model {}
[*]
class User extends Model {}
[*]
[*]
Product::find(); // "Product"
[*]
User::find(); // "User"
[*]?>
<?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
页:
[1]