Home Backend Development PHP Tutorial PHP中的_FILE,CLASS等戏法变量

PHP中的_FILE,CLASS等戏法变量

Jun 13, 2016 am 11:03 AM
class echo function gt lt

PHP中的__FILE,__CLASS等魔术变量

今天看到一个魔术变量,是以前没见过的,__DIR__,我查了查,发现原来是php5.3新增的,顺便举几个例子,解释一下php的魔术变量

1,__FILE__

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

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

  1. /** ?
  2. 在你的公用的配置文件中,来设置你的根目录,这样就不用担心经常搬家了。 ?
  3. */??
  4. define('ROOT_PATH',?dirname(__FILE__)?.?DIRECTORY_SEPARATOR); ??
  5. echo?ROOT_PATH; ??
  6. echo?"
    "
    ; ??
  7. echo?__FILE__; ??
  8. echo?"
    "
    ; ??
  9. echo?dirname(__FILE__); ??
  10. echo?"
    "
    ; ??
  11. echo?dirname(dirname(__FILE__)); ??
  12. ?>??
<?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__

文件中的当前行号。这个变量在调试错误的时候,还是比较有作用的,其他的时候,没什么用处,纯属个人观点。

  1. echo?__LINE__;??//显示,__LINE__所在的行号 ??
  2. ?>??
<?phpecho __LINE__;  //显示,__LINE__所在的行号?>
Copy after login

3,__CLASS__

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

  1. class?base_class ??
  2. { ??
  3. ?function?say_a() ??
  4. ?{ ??
  5. ?echo?"'a'?-?said?the?"?.?__CLASS__?.?"
    "
    ; ??
  6. ?} ??
  7. ?function?say_b() ??
  8. ?{ ??
  9. ?echo?"'b'?-?said?the?"?.?get_class($this)?.?"
    "
    ; ??
  10. ?} ??
  11. } ??
  12. ??
  13. class?derived_class?extends?base_class ??
  14. { ??
  15. ?function?say_a() ??
  16. ?{ ??
  17. ?parent::say_a(); ??
  18. ?echo?"'a'?-?said?the?"?.?__CLASS__?.?"
    "
    ; ??
  19. ?} ??
  20. ?function?say_b() ??
  21. ?{ ??
  22. ?parent::say_b(); ??
  23. ?echo?"'b'?-?said?the?"?.?get_class($this)?.?"
    "
    ; ??
  24. ?} ??
  25. } ??
  26. ??
  27. $obj_b?=?new?derived_class(); ??
  28. $obj_b->say_a(); ??
  29. echo?"
    "
    ; ??
  30. $obj_b->say_b(); ??
  31. ?> ??
  32. 结果为: ??
  33. 'a'?-?said?the?base_class ??
  34. 'a'?-?said?the?derived_class ??
  35. ??
  36. 'b'?-?said?the??derived_class ??
  37. 'b'?-?said?the?derived_class??
<?phpclass 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中返回的结果是区分大小写的

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

  1. class?test ??
  2. { ??
  3. ?function?a() ??
  4. ?{ ??
  5. ?echo?__FUNCTION__; ??
  6. ?echo?"
    "
    ; ??
  7. ?echo?__METHOD__; ??
  8. ?} ??
  9. } ??
  10. ??
  11. function?good?(){ ??
  12. ?echo?__FUNCTION__; ??
  13. ?echo?"
    "
    ; ??
  14. ?echo?__METHOD__; ??
  15. } ??
  16. ??
  17. $test?=?new?test(); ??
  18. $test->a(); ??
  19. echo?"
    "
    ; ??
  20. good(); ??
  21. ?> ??
  22. 返回结果: ??
  23. a ??
  24. test::a ??
  25. good ??
  26. good??
<?phpclass 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();?>返回结果:atest::agoodgood
Copy after login

相对于孤立的函数来说,二个都可以取出函数名,没什么区别,如果是class中的方法时,__FUNCTION__只能取出class的方法名,而__METHOD__不光能取出方法名,还能取出class名

5,__DIR__

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

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

  1. if(!defined('__DIR__'))?{ ??
  2. ?$iPos?=?strrpos(__FILE__,?"/"); ??
  3. ?define("__DIR__",?substr(__FILE__,?0,?$iPos)?.?"/"); ??
  4. } ??
  5. ?>??
<?phpif (!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名。

  1. //php5.3 ??
  2. class?Model ??
  3. { ??
  4. ?public?static?function?find() ??
  5. ?{ ??
  6. ?echo?__STATIC__; ??
  7. ?} ??
  8. } ??
  9. ??
  10. class?Product?extends?Model?{} ??
  11. class?User?extends?Model?{} ??
  12. ??
  13. Product::find();?//?"Product" ??
  14. User::find();?//?"User" ??
  15. ?>??
<?php //php5.3class 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 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)

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)

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.

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

What does class mean in python? What does class mean in python? May 21, 2019 pm 05:10 PM

Class is a keyword in Python, used to define a class. The method of defining a class: add a space after class and then add the class name; class name rules: capitalize the first letter. If there are multiple words, use camel case naming, such as [class Dog()].

Five selected Go language open source projects to take you to explore the technology world Five selected Go language open source projects to take you to explore the technology world Jan 30, 2024 am 09:08 AM

In today's era of rapid technological development, programming languages ​​are springing up like mushrooms after a rain. One of the languages ​​that has attracted much attention is the Go language, which is loved by many developers for its simplicity, efficiency, concurrency safety and other features. The Go language is known for its strong ecosystem with many excellent open source projects. This article will introduce five selected Go language open source projects and lead readers to explore the world of Go language open source projects. KubernetesKubernetes is an open source container orchestration engine for automated

Replace the class name of an element using jQuery Replace the class name of an element using jQuery Feb 24, 2024 pm 11:03 PM

jQuery is a classic JavaScript library that is widely used in web development. It simplifies operations such as handling events, manipulating DOM elements, and performing animations on web pages. When using jQuery, you often encounter situations where you need to replace the class name of an element. This article will introduce some practical methods and specific code examples. 1. Use the removeClass() and addClass() methods jQuery provides the removeClass() method for deletion

Go language development essentials: 5 popular framework recommendations Go language development essentials: 5 popular framework recommendations Mar 24, 2024 pm 01:15 PM

&quot;Go Language Development Essentials: 5 Popular Framework Recommendations&quot; As a fast and efficient programming language, Go language is favored by more and more developers. In order to improve development efficiency and optimize code structure, many developers choose to use frameworks to quickly build applications. In the world of Go language, there are many excellent frameworks to choose from. This article will introduce 5 popular Go language frameworks and provide specific code examples to help readers better understand and use these frameworks. 1.GinGin is a lightweight web framework with fast

See all articles