PHP __DIR__, __FILE__, __FUNCTION__, __CLASS__, __METHOD__, __LINE__, __NAMESPACE__
PHP has large number of predefined constants. This HOWTO will present the seven most important, most practical and most useful PHP Magic Constants.
- __FILE__ – The full path and filename of the file.
- __DIR__ – The directory of the file.
- __FUNCTION__ – The function name.
- __CLASS__ – The class name.
- __METHOD__ – The class method name.
- __LINE__ – The current line number of the file.
- __NAMESPACE__ – The name of the current namespace
This is example PHP script with comments, which demonstrate howto use all previously mentioned PHP Magic Constants.
<?php
// Set namespace
namespace TestProject;
// This prints file full path and anme
echo 'This file full path and file name is ' . __FILE__ . "\n";
// This prints file full path, without file name
echo 'This file full path is ' . __DIR__ . "\n";
// This prints current line number on file
echo 'This is line number ' . __LINE__ . "\n";
function test_function_magic_constant() {
echo 'This is from ' . __FUNCTION__ . "\n";
}
test_function_magic_constant();
class TestMagicConstants {
// prints class name
public function printClassName() {
echo 'This is ' . __CLASS__ . " class. \n";
}
// prints class and method name
public function printMethodName() {
echo 'This is ' . __METHOD__ . " method\n";
}
// print function name
public function printFunctionName() {
echo 'This is function ' . __FUNCTION__ . " inside class\n";
}
// prints namespace name
public function printNamespace() {
echo 'Namespace name is ' . __NAMESPACE__ . "\n";
}
}
$test_magic_constants = new TestMagicConstants;
$test_magic_constants->printClassName();
$test_magic_constants->printMethodName();
$test_magic_constants->printFunctionName();
$test_magic_constants->printNamespace();
RBAC(基于角色的访问控制权限)表结构原理分析
PHP __DIR__, __FILE__, __FUNCTION__, __CLASS__, __METHOD__, __LINE__, __NAMESPACE__的更多相关文章
- 012-对象——魔术常量__CLASS__ __METHOD__ __FUNCTION__ __DIR__ __FILE__
<?php /** *魔术常量__CLASS__ __METHOD__ __FUNCTION__ __DIR__ __FILE__ */ //魔术常量:__CLASS__ 得到类名. /*cla ...
- 34.Linux-printk分析、使用__FILE__, __FUNCTION__, __LINE__ 调试
本节学习目的 1)分析printk()函数 2)使用printk()调试驱动 1.在驱动调试中,使用printk(),是最简单,最方便的办法 当uboot的命令行里的“console=tty1”时,表 ...
- PHP 魔术常量__FUNCTION__与__METHOD__的区别
__FUNCTION__ 返回 函数名称(PHP 4.3.0 新加).自 PHP 5 起本常量返回该函数被定义时的名字(区分大小写).在 PHP 4 中该值总是小写字母的. __METHOD__ ...
- PHP中__FUNCTION__与__METHOD__的区别
你知道php中__FUNCTION__与__METHOD__的区别吗?本文通过一个小例子,为大家介绍下二者的区别,有兴趣的朋友可以参考下. PHP中__FUNCTION__与__METHOD__的 ...
- php之__DIR__,__FILE__,getcwd()的区别。
__DIR__ 在哪个脚本文件里面出现,就显示当前脚本的目录,不包含文件名.假如目录A下的1.php包含了这个魔术常量,这个文件被目录B下的2.php调用了.那么__DIR__返回的值是多少呢?返回的 ...
- PHP基础知识小结
1.PHP中类型转换 自动转换 其它类型转换数值型 true->1 false->0 null->0 'true'->0 '-3abc'->-3 '3.123abc'-& ...
- PHP系统学习1
1.php变量 2.php引用变量 $name1=&$name2; 3.全局变量 4.魔术变量__LINE__,__FILE__,__FUNCTION__,__CLASS__,__METHOD ...
- php __FILE__,__CLASS__等魔术变量,及实例
今天突然看到几个自己不认识的魔术变量 不知道怎么用于是就上网查了一下,看到了这篇博客,写的真不错,希望自己以后也能学会这样总结 张映 发表于 2010-12-13 分类目录: php 标签:php, ...
- PHP问答题大全
答案在题目后面,文字与背景同色,连续单机三次鼠标一行出答案哦: 1.PHP有几种原始数据类型,分别是什么?答:八种,分别是:int,float,string,bool,array,object,res ...
随机推荐
- 通过boundingRectWithSize:options:attributes:context:计算文本尺寸
转:http://blog.csdn.net/iunion/article/details/12185077 之前用Text Kit写Reader的时候,在分页时要计算一段文本的尺寸大小,之前使用 ...
- 用Task代替TheadPool
TheadPool的问题 不支持线程的取消.完成.失败通知等交互性操作 不支持线程执行先后次序 using System; using System.Diagnostics; using System ...
- 微信公共账号开发:48001 api unauthorized解决
微信公众平台开发:获取token成功并且拉取自定义菜单也OK,但是在拉取用户信息和分组信息 是总是出现这个 {"errcode":48001,"errmsg": ...
- 防止WordPress利用xmlrpc.php进行暴力破解以及DDoS
早在2012 年 12 月 17 日一些采用 PHP 的知名博客程序 WordPress被曝光存在严重的漏洞,该漏洞覆盖WordPress 全部已发布的版本(包括WordPress 3.8.1).该漏 ...
- Understanding mysql max_connect_errors
来自:http://mysqlblog.fivefarmers.com/2013/08/08/understanding-max_connect_errors/ Perhaps like many u ...
- YAFFS2文件系统分析(转)
http://blog.chinaunix.net/uid-25314474-id-343665.html 1.前言略. 2.yaffs 文件系统简介按理说这里应该出现一些诸如“yaffs 是一种适合 ...
- CSV - 操作比较
在数据报表生成的时候,我们一般会用程序去生成CSV.其中有些需要注意的地方. log_file = open('delay.%s.csv' % s_end, 'w') log_file.write(' ...
- centos7 Linux 安装mysql
安装过程如: http://blog.csdn.net/tengdazhang770960436/article/details/18361979 中间过程出现无法安装mysql-server 解决方 ...
- 结合仓库设计MVC控制器
为了更高效的开发MVC项目,我们对控制器进行了在一次的封装,使得控制器能够获得很好的继承关系,并能以更少 的代码,实现Web项目的开发工作,整个控制器的设计思路如下所示. 从上图的设计里面 ...
- [SQL SERVER 2005]数据库差异备份及还原
因为之前遇到还原差异备份,最开始遇到SQLServer报错:”无法还原日志备份或差异备份,因为没有文件可用于前滚“.查阅很多资料后,终于得到解决.收集整理成这篇随笔. 问题原因:出现这种错误绝大多数是 ...