1 _LINE_
顯示當前代碼的行數:
echo "This is line number : ". __LINE__;
2 _FILE_
顯示當前檔案的路徑
3 _METHOD_
顯示當前方法的名,比如
class Magicconstant
{
function __construct()
{
echo "This is function a";
}
function b()
{
echo "
";
echo "This is function b";
echo "
";
echo __METHOD__;
}
}
$cm = new Magicconstant();
$cm->b();
顯示
This is function a
This is function b
Magicconstant::b
4 _FUNCTION_
顯示當前所在函數的名
function b()
{
echo "
";
echo "This is function b";
echo "
";
echo __FUNCTION__;
}
輸出:
This is function b
Magicconstant::b
5 _DIR_
顯示目前的目錄名,如
echo "The directory name is : ". __DIR__;
輸出:
The directory name is : D:\wamp\www
6 _CLASS_
顯示當前的類
class Magicconstant
{
function __construct()
{
echo "The class name is : ".__CLASS__;
}
}
$cm = new Magicconstant();
顯示:
The class name is : Magicconstant
7 _NAMESPACE_
顯示當前命名空間
namespace MagicConstant
{
echo "The namespace is : ".__NAMESPACE__;
}
輸出:
The namespace is : MagicConstant
8 _sleep_
_sleep_用在將類序列化之前,
class User
{
public $userName = '';
public $userAddress = '';
public $userPhone = '';
public function setName($name='')
{
$this->userName = $name;
}
function setAddress($address='')
{
$this->userAddress = $address;
}
function setPhone($phone='')
{
$this->userPhone = $phone;
}
function __sleep()
{
return array('userAddress','userPhone');
// This will serialize only address and Phone number but not Name
}
}
?>
$User = new User();
$User->setName('Avinash');
$User->setAddress('Address Here');
$User->setPhone('1234567890');