码迷,mamicode.com
首页 > Web开发 > 详细

PHP __DIR__, __FILE__, __FUNCTION__, __CLASS__, __METHOD__, __LINE__, __NAMESPACE__

时间:2014-09-25 21:58:27      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:style   color   io   os   ar   strong   for   sp   cti   

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__

标签:style   color   io   os   ar   strong   for   sp   cti   

原文地址:http://www.cnblogs.com/wanghetao/p/3993571.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!