php的魔术变量__METHOD__、__FUNCTION、__DIR__、__f
来源:互联网 发布:Windows 10 无线网络 编辑:程序博客网 时间:2024/06/14 08:38
在php中提供了__FILE__、__DIR__、__LINE__、__CLASS__、__NAMESPACE__、__METHOD__、__FUNCTION__等魔术变量,其中:
__FILE__:返回该文件的完整路径和文件名。
__DIR__:返回文件的目录。
__LINE__:返回当前文件的行数。
__CLASS__:返回类名。
__NAMESPACE__:返回当前命名空间的名称。
__METHOD__:返回类的方法名。
__FUNCTION__:返回当前函数名。
<?php// Set namespace (works only with PHP 5.3+)namespace TestProject;// This prints file full path and nameecho "This file full path and file name is '" . __FILE__ . "'.\n";// This prints file full path, without file nameecho "This file full path is '" . __DIR__ . "'.\n";// This prints current line number on fileecho "This is line number " . __LINE__ . ".\n";// Really simple basic test functionfunction test_function_magic_constant() {echo "This is from '" . __FUNCTION__ . "' function.\n";}// Prints function and used namespacetest_function_magic_constant();// Really simple class for testing magic constantsclass TestMagicConstants {// Prints class namepublic function printClassName() {echo "This is " . __CLASS__ . " class.\n";}// Prints class and method namepublic function printMethodName() {echo "This is " . __METHOD__ . " method.\n";}// Prints function namepublic function printFunction() {echo "This is function '" . __FUNCTION__ . "' inside class.\n";}// Prints namespace name (works only with PHP 5.3)public function printNamespace() {echo "Namespace name is '" . __NAMESPACE__ . "'.\n";}}// Create new TestMagicConstants class$test_magic_constants = new TestMagicConstants;// This prints class name and used namespace$test_magic_constants->printClassName();// This prints method name and used namespace$test_magic_constants->printMethodName();// This prints function name inside class and used namespace// same as method name, but without class$test_magic_constants->printFunction();// This prints namespace name (works only with PHP 5.3)$test_magic_constants->printNamespace();?>输出结果:
This file full path and file name is '/tmp/magic_constants/magic.php'.This file full path is '/tmp/magic_constants'.This is line number 13.This is from 'TestProject\test_function_magic_constant' function.This is TestProject\TestMagicConstants class.This is TestProject\TestMagicConstants::printMethodName method.This is function 'printFunction' inside class.Namespace name is 'TestProject'.
这就是php中的相关魔术变量,在php的相关开发中,使用魔术变量将会使得开发变得更加的简便。
阅读全文
0 0
- php的魔术变量__METHOD__、__FUNCTION、__DIR__、__f
- PHP的__DIR__变量!!
- php __FILE__ __DIR__魔术常量的使用
- php __FILE__ __DIR__魔术常量的使用【PHP进阶教程】
- php 中魔术常量 __FUNCTION__ 和 __METHOD__
- PHP 5.3中新增的魔术常量__DIR__
- php的魔术变量
- PHP的魔术变量及魔术方法
- PHP5.3中新增的魔术常量__DIR__
- PHP5.3中新增的魔术常量__DIR__
- PHP5.3中新增的魔术常量__DIR__
- PHP5.3中新增的魔术常量__DIR__
- PHP5.3中新增的魔术常量__DIR__
- php __FUNCTION__ __METHOD__ 区别
- PHP魔术变量
- PHP魔术变量
- php魔术变量-重载
- PHP魔术变量
- 框架漫谈之spring(一)工厂模式实现程序解耦,spring框架的引出
- Oracle expdp impdp导出导入命令及数据库备份
- 队列
- 共轭梯度法
- 有功功率、无功功率、视在功率、功率因数及峰值因子
- php的魔术变量__METHOD__、__FUNCTION、__DIR__、__f
- VMware Tools 安装方法
- mysql索引设计
- Redis的过期键删除策略
- 328. Odd Even Linked List
- 设计模式之单例模式
- C#中的虚方法总结--1
- 静态代理
- 我的github page