PHP 戏法常量

Jun 13, 2016 pm 01:18 PM
class echo file function gt

PHP 魔术常量

1,__FILE__

文件的完整路径和文件名。如果用在被包含文件中,则返回被包含的文件名。自 PHP 4.0.2 起,__FILE__?总是包含一个绝对路径(如果是符号连接,则是解析后的绝对路径),而在此之前的版本有时会包含一个相对路径。
这个变量,我用的是最多的,估计也是大家用的最多的。

web服务器都会指定一个documentroot的,但是不同的服务器,设置的documentroot有可能是不同的,在这种情况下,把一个网站从一个服务器搬家到另一个服务器,这样就有可能因为路径的不同,造成网站跑不起来。

<?php  
/** 
在你的公用的配置文件中,来设置你的根目录,这样就不用担心经常搬家了。 
*/  
define('ROOT_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);  
echo ROOT_PATH;  
echo "<br>";  
echo __FILE__;  
echo "<br>";  
echo dirname(__FILE__);  
echo "<br>";  
echo dirname(dirname(__FILE__));  
?>  
Copy after login
?2,__LINE__

文件中的当前行号。

<?php  
echo __LINE__;  //显示,__LINE__所在的行号  
?> 
Copy after login
?3,__CLASS__

类的名称,PHP5返回的结果是区分大小写的

<?php  
class base_class  
{  
 function say_a()  
 {  
 echo "'a' - said the " . __CLASS__ . "<br/>";  
 }  
 function say_b()  
 {  
 echo "'b' - said the " . get_class($this) . "<br/>";  
 }  
}  
  
class derived_class extends base_class  
{  
 function say_a()  
 {  
 parent::say_a();  
 echo "'a' - said the " . __CLASS__ . "<br/>";  
 }  
 function say_b()  
 {  
 parent::say_b();  
 echo "'b' - said the " . get_class($this) . "<br/>";  
 }  
}  
  
$obj_b = new derived_class();  
$obj_b->say_a();  
echo "<br/>";  
$obj_b->say_b();  
?>  
结果为:  
'a' - said the base_class  
'a' - said the derived_class  
  
'b' - said the  derived_class  
'b' - said the derived_class  
Copy after login
?有的时候,我们可以用get_class来代替__CLASS__

4,__FUNCTION__和__METHOD__

__FUNCTION__:函数名称,php5中返回的结果是区分大小写的
__METHOD__:方法中的函数名称,php5中返回的结果是区分大小写的

二个都是取得方法的名称,有什么不同呢?

<?php  
class test  
{  
 function a()  
 {  
 echo __FUNCTION__;  
 echo "<br>";  
 echo __METHOD__;  
 }  
}  
  
function good (){  
 echo __FUNCTION__;  
 echo "<br>";  
 echo __METHOD__;  
}  
  
$test = new test();  
$test->a();  
echo "<br>";  
good();  
?>  
返回结果:  
a  
test::a  
good  
good 
Copy after login
?相对于孤立的函数来说,二个都可以取出函数名,没什么区别,如果是class中的方法时,__FUNCTION__只能取出class的方法名,而__METHOD__不光能取出方法名,还能取出class名

5,__DIR__

文件所在的目录。如果用在被包括文件中,则返回被包括的文件所在的目录。它等价于 dirname(__FILE__)。除非是根目录,否则目录中名不包括末尾的斜杠。(PHP 5.3.0中新增)

如果在5.3以前的版本中想用__DIR__的话,可以这样

<?php  
if(!defined('__DIR__')) {  
 $iPos = strrpos(__FILE__, "/");  
 define("__DIR__", substr(__FILE__, 0, $iPos) . "/");  
}  
?> 
Copy after login
?6,__NAMESPACE__

当前命名空间的名称(大小写敏感)。这个常量是在编译时定义的(PHP 5.3.0 新增)

7,__STATIC__

当你调用class的静态方法时,返回class名称,区分大小写。如果在继承中调用的话,不管在继承中有没有定义,都能返回继承的class名。

<?php  
//php5.3  
class Model  
{  
 public static function find()  
 {  
 echo __STATIC__;  
 }  
}  
  
class Product extends Model {}  
class User extends Model {}  
  
Product::find(); // "Product"  
User::find(); // "User"  
?>
Copy after login
?
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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1668
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

How to use classes and methods in Python How to use classes and methods in Python Apr 21, 2023 pm 02:28 PM

Concepts and instances of classes and methods Class (Class): used to describe a collection of objects with the same properties and methods. It defines the properties and methods common to every object in the collection. Objects are instances of classes. Method: Function defined in the class. Class construction method __init__(): The class has a special method (construction method) named init(), which is automatically called when the class is instantiated. Instance variables: In the declaration of a class, attributes are represented by variables. Such variables are called instance variables. An instance variable is a variable modified with self. Instantiation: Create an instance of a class, a specific object of the class. Inheritance: that is, a derived class (derivedclass) inherits the base class (baseclass)

How to convert php blob to file How to convert php blob to file Mar 16, 2023 am 10:47 AM

How to convert php blob to file: 1. Create a php sample file; 2. Through "function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })} ” method can be used to convert Blob to File.

Use java's File.length() function to get the size of the file Use java's File.length() function to get the size of the file Jul 24, 2023 am 08:36 AM

Use Java's File.length() function to get the size of a file. File size is a very common requirement when dealing with file operations. Java provides a very convenient way to get the size of a file, that is, using the length() method of the File class. . This article will introduce how to use this method to get the size of a file and give corresponding code examples. First, we need to create a File object to represent the file we want to get the size of. Here is how to create a File object: Filef

Hongmeng native application random poetry Hongmeng native application random poetry Feb 19, 2024 pm 01:36 PM

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

Rename files using java's File.renameTo() function Rename files using java's File.renameTo() function Jul 25, 2023 pm 03:45 PM

Use Java's File.renameTo() function to rename files. In Java programming, we often need to rename files. Java provides the File class to handle file operations, and its renameTo() function can easily rename files. This article will introduce how to use Java's File.renameTo() function to rename files and provide corresponding code examples. The File.renameTo() function is a method of the File class.

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

See all articles