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. Android:改变Activity切换方式(转载)

    overridePendingTransition(enterAnim, exitAnim); Intent intent =new Intent(this,item2.class); startAc ...

  2. linux用shell腳本解决被ddos攻击的问题

    最近网站常常被人DDOS所以写了一个小程序用来自动封锁IP,代码如下: !/bin/bash for (( ; ; )) do status=netstat -na|grep ESTABLISHED| ...

  3. QQ邮箱的安全问题

    下午同事群里有人提醒,小心欺诈邮件.邮件内容为你的帐户在XX存在异地登录,已经进入了[保护模式],如需解除请点击[解除保护模式] 除了链接之外,其它跟官方的是一模一样,包括标题. 那个链接的地址是:h ...

  4. AngularJS datepicker 和 datatimepicker

    本文内容 项目结构 AngularJS datepicker AngularJS+jQueryUI datetimepicker 本文介绍 AngualrJS datetimepicker 控件.说明 ...

  5. Redmine性能优化方案

    近来公司redmine服务器表现很糟糕,在16核,64GRAM的机器上,压测结果竟然只有每秒5~7个请求,部分页面一个都出不来. 以下是我对Redmine性能优化方案: redmine服务器性能问题排 ...

  6. WCF 数据服务 4.5

    .NET Framework 4.5 其他版本 WCF 数据服务(以前称为"ADO.NET Data Services")是 .NET Framework 的一个组件.可以使用此组 ...

  7. 在Oracle Linux Server release 6.4下配置ocfs2文件系统

    ① 安装ocfs-tools-1.8 如果是使用RedHat Enterprise Linux 6.4,也可以安装ocfs-tools-1.8的,只是要插入Oracle Linux Server re ...

  8. ASP.NET 4.0 forms authentication issues with IE11

    As I mentioned earlier, solutions that rely on User-Agent sniffing may break, when a new browser or ...

  9. Request is not available in this context

    部署到新服务器的IIS的时候发现这个错误: Request is not available in this context 解决方案: <system.web> <customEr ...

  10. URL 学习总结

    1.绝对路径(以"/"斜线开头的路径,代表相对于当前Web应用): a)地址给服务器用,web应用名称可以省略. 请求包含:request.getRequestDispatcher ...