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__的更多相关文章

  1. 012-对象——魔术常量__CLASS__ __METHOD__ __FUNCTION__ __DIR__ __FILE__

    <?php /** *魔术常量__CLASS__ __METHOD__ __FUNCTION__ __DIR__ __FILE__ */ //魔术常量:__CLASS__ 得到类名. /*cla ...

  2. 34.Linux-printk分析、使用__FILE__, __FUNCTION__, __LINE__ 调试

    本节学习目的 1)分析printk()函数 2)使用printk()调试驱动 1.在驱动调试中,使用printk(),是最简单,最方便的办法 当uboot的命令行里的“console=tty1”时,表 ...

  3. PHP 魔术常量__FUNCTION__与__METHOD__的区别

    __FUNCTION__ 返回 函数名称(PHP 4.3.0 新加).自 PHP 5 起本常量返回该函数被定义时的名字(区分大小写).在 PHP 4 中该值总是小写字母的.    __METHOD__ ...

  4. PHP中__FUNCTION__与__METHOD__的区别

    你知道php中__FUNCTION__与__METHOD__的区别吗?本文通过一个小例子,为大家介绍下二者的区别,有兴趣的朋友可以参考下.   PHP中__FUNCTION__与__METHOD__的 ...

  5. php之__DIR__,__FILE__,getcwd()的区别。

    __DIR__ 在哪个脚本文件里面出现,就显示当前脚本的目录,不包含文件名.假如目录A下的1.php包含了这个魔术常量,这个文件被目录B下的2.php调用了.那么__DIR__返回的值是多少呢?返回的 ...

  6. PHP基础知识小结

    1.PHP中类型转换 自动转换 其它类型转换数值型 true->1 false->0 null->0 'true'->0 '-3abc'->-3 '3.123abc'-& ...

  7. PHP系统学习1

    1.php变量 2.php引用变量 $name1=&$name2; 3.全局变量 4.魔术变量__LINE__,__FILE__,__FUNCTION__,__CLASS__,__METHOD ...

  8. php __FILE__,__CLASS__等魔术变量,及实例

    今天突然看到几个自己不认识的魔术变量 不知道怎么用于是就上网查了一下,看到了这篇博客,写的真不错,希望自己以后也能学会这样总结 张映 发表于 2010-12-13 分类目录: php 标签:php,  ...

  9. PHP问答题大全

    答案在题目后面,文字与背景同色,连续单机三次鼠标一行出答案哦: 1.PHP有几种原始数据类型,分别是什么?答:八种,分别是:int,float,string,bool,array,object,res ...

随机推荐

  1. 我用了13行代碼開發出来的PHP框架

    我只用13行代碼開發的PHP框架,如果您對框架不理解,不知道框架究竟幫您做了什麽事,可以下載此框架看一下, 另外如果您想開發自己的框架也可以由這個框架的思路進行擴展. 源碼下載地址:http://do ...

  2. 在线视频转gif动画工具 在线视频转gif动画工具下载

    在线视频转gif动画工具 在线视频转gif动画工具下载 http://www.leawo.cn/space-1723875-do-thread-id-60715.html http://www.lea ...

  3. GTD中定位篇

    一:为什么要定位? 每天我们的大脑涌现很多想法和要处理很多事情,如果我们没有一套流模式处理这些想法和事情,我们大脑将会处于混战忙碌中,很快就被淹没. 定位的目的: 就是有一套流模式有序的分界我们想法和 ...

  4. PHP —— 读取文件到二维数组

    转自:PHP读取自定义ini文件到二维数组 读取文件,可以使用file_get_contents,file,parse_ini_file等,现在有一个需求,需要读取如下格式的文件: [food] ap ...

  5. Scala 具体的并行集合库【翻译】

    原文地址 本文内容 并行数组(Parallel Array) 并行向量(Parallel Vector) 并行范围(Parallel Range) 并行哈希表(Parallel Hash Tables ...

  6. nginx-upstream-keepalive;accept_mutex-proxy_http_version-1.1-proxy_set_header-connection

    nginx+resin+redis+mysql的架构: 有外部模拟用户请求的大量攻击:它尝试请求了80就断开:看到的现象是在跑有nginx80的主机上TCP连接数少 : ESTABLISHED少: 在 ...

  7. Ubuntu server解决不能访问外网问题

    Ubuntu server解决不能访问外网问题 在Ubuntu Server上设置访问外网时,需要设置dns,通常是将dns添加到/etc/resolv.conf文件中. 但是将dns添加至/etc/ ...

  8. 【转帖】Moodle平台的5个新玩法

    [玩坏Moodle平台]Moodle平台的5个新玩法 1.RSS订阅 Moodle平台可以导入外部博客(或其他提供RSS的服务),并显示在Moodle内置的博客系统中.无论是自己的个人网站还是他人的博 ...

  9. C# 字典排序Array.Sort

    Array.Sort可以实现便捷的字典排序,但如果完全相信他,那么就容易产生些异常!太顺利了,往往是前面有坑等你. 比如:微信接口,好多地方需要签名认证,签名的时候需要用的字典排序,如果只用Array ...

  10. 关于创建可执行的jar文件(assembly)

    java利用maven生成一个jar包,如何自动生成清单属性文件(MANIFEST.MF),如何解决jar依赖问题? 办法很简单: 只需在pom.xml文件中配置如下plugin即可: <plu ...