今天看到一個魔術變量,是以前沒見過的,__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 the derived_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