Home Backend Development PHP Tutorial Detailed explanation of magic constant examples in php

Detailed explanation of magic constant examples in php

May 03, 2017 pm 05:33 PM

What are magic constants?

When explaining PHP constants, we know that most of the constants in PHP are unchanged, but there are 8 predefined constants that will change as the location of the code where they are located changes. These 8 A constant is called a magic constant. These special constants are not case-sensitive.

PHP's "magic constants" are as follows:

##FunctionDetection type__LINE__The current line number in the file. __FILE__The full path and file name of the file. If used within an included file, returns the name of the included file. Since PHP 4.0.2, __FILE__ always contains an absolute path (or the resolved absolute path in the case of a symbolic link), while versions before that sometimes contained a relative path. __DIR__The directory where the file is located. If used within an included file, returns the directory where the included file is located. It is equivalent to dirname(__FILE__). Directory names do not include the trailing slash unless they are the root directory. (New in PHP 5.3.0) __FUNCTION__ Function name (New in PHP 4.3.0). Since PHP 5 this constant returns the name of the function as it was defined (case sensitive). In PHP 4 this value is always lowercase. __CLASS__The name of the class (newly added in PHP 4.3.0). Since PHP 5 this constant returns the name of the class when it was defined (case sensitive). In PHP 4 this value is always lowercase. The class name includes the scope in which it is declared (e.g. Foo\Bar). Note that since PHP 5.4 __CLASS__ also works for traits. When used in a trait method, __CLASS__ is the name of the class that calls the trait method __TRAIT__The name of the Trait (new in PHP 5.4.0). Since PHP 5.4 this constant returns the name of the trait as it was defined (case-sensitive). The Trait name includes the scope in which it is declared (for example, Foo\Bar)__METHOD__The method name of the class (newly added in PHP 5.0.0). Returns the name of the method as it was defined (case-sensitive). __NAMESPACE__The name of the current namespace (case-sensitive). This constant is defined at compile time (new in PHP 5.3.0).

Detailed explanation of magic constant examples in phpThe "__" in the above table is two underscores, not one "_".

Magic constants are often used to obtain current environment information or record logs.

Let me take a look at the usage of these magic constants:

__LINE__ Get the current line number in the file.

Example

<?PHP
header("content-type:text/html;charset=utf-8");
echo "这是第 ". __LINE__ ."行";
echo "<br/>";
?>
Copy after login

Code running result:

Detailed explanation of magic constant examples in php

__FILE__ Get the full path and file name of the file, if When used in an included file, returns the included file name.

Detailed explanation of magic constant examples in php

__DIR__ Get the directory where the file is located. If used within an included file, returns the directory where the included file is located.

Example

<?php
header("content-type:text/html;charset=utf-8");
echo "该文件位于 ". __DIR__ ."文件夹下";
echo "<br/>";
?>
Copy after login

Code running result:

Detailed explanation of magic constant examples in php

__FUNCTION__ Get the name of the function when it is defined (differentiate uppercase and lowercase), only the function name

instance

<?php
header("content-type:text/html;charset=utf-8");
function Test() {
    echo  "该函数名为:". __FUNCTION__ ;
    echo "<br/>";
}
Test();
?>
Copy after login

code running result is returned:

Detailed explanation of magic constant examples in php

__CLASS__ Get the The name when the class is defined (case-sensitive)

Example

<?php
header("content-type:text/html;charset=utf-8");
class test {
    function show() {
        echo "该类名为:". __CLASS__ . "<br>";
        echo "该函数名为:". __FUNCTION__ ;
        echo "<br/>";
    }
}
$t = new test();
$t->show();
?>
Copy after login

Code running result:

Detailed explanation of magic constant examples in php

__METHOD__ Get the name of the method when it is defined (case-sensitive). If the function is in a class, return the class method name,

That is: Class name::Method name.

Example

<?php
    header("content-type:text/html;charset=utf-8");
    function test1() {
        echo  "该函数名为:". __METHOD__ ;
        echo "<br/>";
    }
    test1();
    ?>
    <?php
    class chhua{
        function test(){
            //返回类方法的名称
            echo __METHOD__;
        }
    }
    $e=new chhua();
    $e->test();//输出:chhua::test
    ?>
Copy after login

Code running result:

Detailed explanation of magic constant examples in php

##__NAMESPACE__ The name of the current namespace (case-sensitive)

<?php
namespace MyProject;

echo &#39;"&#39;, __NAMESPACE__, &#39;"&#39;; // 输出 "MyProject"
?>
Copy after login
Code running result:

Detailed explanation of magic constant examples in php

The above is the detailed content of Detailed explanation of magic constant examples in php. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are constants in C language? Can you give an example? What are constants in C language? Can you give an example? Aug 28, 2023 pm 10:45 PM

A constant is also called a variable and once defined, its value does not change during the execution of the program. Therefore, we can declare a variable as a constant referencing a fixed value. It is also called text. Constants must be defined using the Const keyword. Syntax The syntax of constants used in C programming language is as follows - consttypeVariableName; (or) consttype*VariableName; Different types of constants The different types of constants used in C programming language are as follows: Integer constants - For example: 1,0,34, 4567 Floating point constants - Example: 0.0, 156.89, 23.456 Octal and Hexadecimal constants - Example: Hex: 0x2a, 0xaa.. Octal

How to create a constant in Python? How to create a constant in Python? Aug 29, 2023 pm 05:17 PM

Constants and variables are used to store data values ​​in programming. A variable usually refers to a value that can change over time. A constant is a type of variable whose value cannot be changed during program execution. There are only six built-in constants available in Python, they are False, True, None, NotImplemented, Ellipsis(...) and __debug__. Apart from these constants, Python does not have any built-in data types to store constant values. Example An example of a constant is demonstrated below - False=100 outputs SyntaxError:cannotassigntoFalseFalse is a built-in constant in Python that is used to store boolean values

In Java, is it possible to define a constant using only the final keyword? In Java, is it possible to define a constant using only the final keyword? Sep 20, 2023 pm 04:17 PM

A constant variable is a variable whose value is fixed and only one copy exists in the program. Once you declare a constant variable and assign a value to it, you cannot change its value again throughout the program. Unlike other languages, Java does not directly support constants. However, you can still create a constant by declaring a variable static and final. Static - Once you declare a static variable, they will be loaded into memory at compile time, i.e. only one copy will be available. Final - Once you declare a final variable, its value cannot be modified. Therefore, you can create a constant in Java by declaring the instance variable as static and final. Example Demonstration classData{&am

PHP error: How to solve the problem when calling undefined constant? PHP error: How to solve the problem when calling undefined constant? Aug 26, 2023 pm 03:39 PM

PHP is a server-side scripting language widely used in web development. Its flexibility and ease of use make it the first choice for many developers. However, when using PHP, we sometimes encounter some error reports. This article will focus on the "Call to undefined constant" error and how to resolve it. 1. Problem Description When we use an undefined constant in the code, PHP will throw a fatal error, prompting us to call an undefined constant. Here is a common example: echoMY_

This article will help you understand variables and constants in Python This article will help you understand variables and constants in Python Jul 25, 2023 pm 02:15 PM

This article is based on the basics of Python. It mainly introduces the difference between variables and constants in the basics of Python. It explains the usage of variables in detail, and uses rich cases and code renderings to help everyone understand better.

Definition and initialization methods of basic data type constants study guide Definition and initialization methods of basic data type constants study guide Jan 05, 2024 pm 02:08 PM

To learn the definition and initialization method of basic data type constants, specific code examples are required. In programming, various basic data types are often used, such as integers, floating point types, character types, etc. When using these data types, you not only need to understand their definition and usage, but also how to define and initialize their constants. This article will introduce you to the definition and initialization method of basic data type constants, and give specific code examples. Definition and initialization method of integer constants Integer constants include int, long, short and byt

PHP error: What should I do if I use an undefined constant as a property name? PHP error: What should I do if I use an undefined constant as a property name? Aug 17, 2023 pm 02:13 PM

PHP error: What should I do if I use an undefined constant as a property name? In PHP development, we often use classes and objects to organize and manage code. In the process of defining a class, the attributes of the class (i.e. member variables) play an important role in saving data. However, when we use properties, sometimes an error occurs when using undefined constants as property names. This article explains the causes of this error and provides several solutions. First, let's look at a simple example to demonstrate this problem. Suppose we have a file named "Per

What are magic constants in PHP? What are magic constants in PHP? May 20, 2023 pm 04:10 PM

With the continuous development of Internet technology, the demand for websites and applications is increasing. Among them, PHP, as an important tool for web development, is gradually becoming well-known. In PHP, we often hear something about "magic constants", so what are magic constants in PHP? Magic constants are predefined constants whose values ​​are determined when the PHP script is compiled. Magic constants start and end with two underscores, such as LINE__, __FILE and CLA

See all articles