search
HomeBackend DevelopmentPHP TutorialPHP魔术函数、魔术常量、预定义常量使用详解(带例子)

 

PHP魔术函数、魔术常量、预定义常量

一、魔术函数(13个)

__construct()

实例化对象时被调用, 当__construct和以类名为函数名的函数同时存在时,__construct将被调用,另一个不被调用。

__destruct()

当删除一个对象或对象操作终止时被调用。

__call()

对象调用某个方法, 若方法存在,则直接调用;若不存在,则会去调用__call函数。

__get()

读取一个对象的属性时,若属性存在,则直接返回属性值; 若不存在,则会调用__get函数。

__set()

设置一个对象的属性时, 若属性存在,则直接赋值;

若不存在,则会调用__set函数。

__toString()

打印一个对象的时被调用。如echo $obj;或print $obj;

__clone()

克隆对象时被调用。如:$t=new Test();$t1=clone $t;

__sleep()

serialize之前被调用。若对象比较大,想删减一点东东再序列化,可考虑一下此函数。

__wakeup()

unserialize时被调用,做些对象的初始化工作。

__isset()

检测一个对象的属性是否存在时被调用。如:isset($c->name)。

__unset()

unset一个对象的属性时被调用。如:unset($c->name)。

__set_state()

调用var_export时,被调用。用__set_state的返回值做为var_export的返回值。

__autoload()

实例化一个对象时,如果对应的类不存在,则该方法被调用。

举例说明

1、__get() 当试图读取一个并不存在的属性的时候被调用。

如果试图读取一个对象并不存在的属性的时候,PHP就会给出错误信息。如果在类里添加__get方法,并且我们可以用这个函数实现类似java中反射的各种操作。

class Test
{
    public function __get($key)
    {
         echo $key . " 不存在";
    }
}
 
$t = new Test();
echo $t->name;
就会输出:name 不存在

2、__set() 当试图向一个并不存在的属性写入值的时候被调用。

class Test
{
    public function __set($key, $value)
    {
         echo '对' . $key . "附值" . $value;
    }
}
 
$t = new Test(); 
$t->name = "aninggo"; 
 
就会输出:对 name 附值 aninggo

3、__call() 当试图调用一个对象并不存在的方法时,调用该方法。

class Test
{
    public function __call($Key, $Args)
    {
         echo "您要调用的 {$Key} 方法不存在。你传入的参数是:" . print_r($Args, true);
    }
}
 
$t = new Test();
$t->getName(aning, go);
 
程序将会输出:
您要调用的 getName 方法不存在。参数是:Array
(
     [0] => aning
     [1] => go
)
 

4、__toString() 当打印一个对象的时候被调用,这个方法类似于java的toString方法,当我们直接打印对象的时候回调用这个函数。

class Test 
{ 
     public function __toString() 
     { 
         return "打印 Test"; 
     } 
} 
 
$t = new Test(); 
echo $t; 
运行echo $t;的时候,就会调用$t->__toString();从而程序将会输出:打印 Test;

5、__clone() 当对象被克隆时,被调用。

class Test 
{ 
     public function __clone() 
     { 
         echo "我被复制了!"; 
     }
}
 
$t = new Test(); 
$t1 = clone $t;
 
程序输出:我被复制了!

二、魔术常量(8个)

LINE

返回文件中的当前行号。

FILE

返回文件的完整路径和文件名。如果用在包含文件中,则返回包含文件名。自 PHP 4.0.2 起,FILE 总是包含一个绝对路径,而在此之前的版本有时会包含一个相对路径。

DIR

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

FUNCTION

返回函数名称(PHP 4.3.0 新加)。自 PHP 5 起本常量返回该函数被定义时的名字(区分大小写)。在 PHP 4 中该值总是小写字母的。

CLASS

返回类的名称(PHP 4.3.0 新加)。自 PHP 5 起本常量返回该类被定义时的名字(区分大小写)。在 PHP 4 中该值总是小写字母的。

TRAIT

Trait 的名字(PHP 5.4.0 新加)。自 PHP 5.4 起此常量返回 trait 被定义时的名字(区分大小写)。Trait 名包括其被声明的作用区域(例如 Foo\Bar)。

METHOD

返回类的方法名(PHP 5.0.0 新加)。返回该方法被定义时的名字(区分大小写)。 格式:类名::方法名

NAMESPACE

当前命名空间的名称(区分大小写)。此常量是在编译时定义的(PHP 5.3.0 新增)

 

三、预定义常量

PHP_VERSION PHP 程序的版本,如4.0.2
PHP_OS 执行PHP解释器的操作系统名称,如Windows
PHP_SAPI 用来判断是使用命令行还是浏览器执行的,如果 PHP_SAPI=='cli' 表示是在命令行下执行

E_ERROR 最近的错误处
E_WARNING 最近的警告处
E_PARSE 剖析语法有潜在问题处
E_NOTICE 发生不寻常但不一定是错误处

PHP_EOL 系统换行符,Windows是(\r\n),Linux是(/n),MAC是(\r),自 PHP 4.3.10 和 PHP 5.0.2 起可用
DIRECTORY_SEPARATOR 系统目录分隔符,Windows是反斜线(\),Linux是斜线(/)
PATH_SEPARATOR 多路径间分隔符,Windows是反斜线(;),Linux是斜线(:)

PHP_INT_MAX INT最大值,32位平台时值为2147483647,自 PHP 4.4.0 和 PHP 5.0.5 起可用
PHP_INT_SIZE INT字长,32位平台时值为4(4字节),自 PHP 4.4.0 和 PHP 5.0.5 起可用

 

四、PHP运行环境检测函数php_sapi_name()

该函数返回一个描述PHP与WEB服务器接口的小写字符串。

返回描述 PHP 所使用的接口类型(the Server API, SAPI)的小写字符串。

例如,CLI 的 PHP 下这个字符串会是 "cli",Apache 下可能会有几个不同的值,取决于具体使用的 SAPI。
以下列出了可能的值:

aolserver、apache、 apache2filter、apache2handler、 caudium、cgi (直到 PHP 5.3), cgi-fcgi、cli、 continuity、embed、 isapi、litespeed、 milter、nsapi、 phttpd、pi3web、roxen、 thttpd、tux 和 webjames。

SAPI: 服务器端API,貌似和CGI是一个东西。每个服务器提供的API可能不同,但是他们都提供了CGI。

所以可以理解CGI是每个服务器都应该有的SAPI。apache有自己的SAPI,IIS也有自己的。但是php能在这些不同的服务器端工作,因为php支持了它们各自的SAPI。

PHP-CLI: php命令行接口,php可以工作在这种模式下也可以CGI模式。是SAPI的一种,它和CGI提供的功能差不多。


Statement
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
PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP: The Foundation of Many WebsitesPHP: The Foundation of Many WebsitesApr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

Beyond the Hype: Assessing PHP's Role TodayBeyond the Hype: Assessing PHP's Role TodayApr 12, 2025 am 12:17 AM

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

What are Weak References in PHP and when are they useful?What are Weak References in PHP and when are they useful?Apr 12, 2025 am 12:13 AM

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

Explain the __invoke magic method in PHP.Explain the __invoke magic method in PHP.Apr 12, 2025 am 12:07 AM

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools