search
HomeBackend DevelopmentPHP TutorialMagic Methods in PHP5_PHP Tutorial
Magic Methods in PHP5_PHP TutorialJul 14, 2016 am 10:08 AM
phpphp5byusemethodVersionkindRegulationmagic

From PHP 5 onwards, classes in PHP can use magic methods. It stipulates that methods starting with two underscores (__) are reserved as magic methods, so it is recommended that function names do not start with __ unless it is to overload an existing magic method.

1. __get, __set
These two methods are designed for properties that are not declared in the class and their parent class.
 ◆__get( $property ) This method will be triggered when an undefined property is called, and the parameter passed is the name of the property being accessed.
 ◆__set( $property, $value ) When assigning a value to an undefined property, this method will be triggered, and the parameters passed are the property name and value to be set.
The non-declarations here include attributes whose access control is protected and private (that is, attributes that have no permission to access) when called using objects.
2. __isset, __unset
 ◆__isset( $property ) This method is called when the isset() function is called on an undefined property.
 ◆__unset( $property ) This method is called when the unset() function is called on an undefined property.
Same as the __get method and __set method, the undeclared here includes attributes whose access control is protected and private (that is, attributes that have no permission to access) when called using an object.
3. __call
 __call( $method, $arg_array ) This method is called when calling an undefined method.
The undefined methods here include methods that do not have permission to access; if the method does not exist, go to the parent class to find the method. If it does not exist in the parent class, call the __call() method of this class. If the __call() method does not exist in this class, go to the __call() method in the parent class.
4. __autoload
__autoload function, which is automatically called when trying to use a class that has not been defined yet. By calling this function, the scripting engine has a last chance to load the required classes before PHP fails with an error.
If you want to define a global autoloading class, you must use the spl_autoload_register() method to register the processing class to the PHP standard library:
[php]
class Loader
{                                                  
static function autoload_class($class_name) {                                                            
//Find the correct $class_name class and introduce it, if not, an exception will be thrown.
}                                                                      
}
/**
* Set up automatic loading of objects
* spl_autoload_register — Register given function as __autoload() implementation
*/
spl_autoload_register(array(‘Loader’, ‘autoload_class’));
$a = new Test();//Test is instantiated without require to achieve automatic loading. Many frameworks use this method to automatically load classes
class Loader
{                                              
static function autoload_class($class_name) {                                                        
//Find the correct $class_name class and introduce it, if not, an exception will be thrown
}  
}
/**
* Set up automatic loading of objects
* spl_autoload_register — Register given function as __autoload() implementation
*/
spl_autoload_register(array(‘Loader’, ‘autoload_class’));
$a = new Test();//Test is instantiated without require to achieve automatic loading. Many frameworks use this method to automatically load classes
Note: Exceptions thrown in the __autoload function cannot be caught by the catch statement block and cause fatal errors, so they should be caught in the function itself.
5. __construct, __destruct
 ◆__construct constructor, this method is called when an object is created. The advantage of using this method compared to PHP4 is that the constructor can have a unique name, no matter what the name of the class it is in. In this way, you When you change the name of a class, you don't need to change the name of the constructor.
 ◆__destruct destructor method, PHP will call this method before the object is destroyed (that is, before it is cleared from memory). By default, PHP only releases the memory occupied by object properties and destroys object-related resources. The destructor allows you to execute arbitrary code to clear memory after using an object. When PHP decides that your script is no longer associated with the object, the destructor will be called.
Within a function’s namespace, this happens when the function returns. For global variables, this happens at the end of the script. If you want to explicitly destroy an object, you can assign any other value to the variable pointing to the object. Usually assign the variable to NULL or call unset.
6. __clone
Object assignment in PHP 5 uses reference assignment. If you want to copy an object, you need to use the clone method. When calling this method, the object will automatically call the __clone magic method. If the object is copied, certain initialization operations need to be performed. , can be implemented in the __clone method.
7.__toString
The __toString method is automatically called when converting an object into a string, such as when using echo to print the object.
If the class does not implement this method, the object cannot be printed through echo, otherwise it will display: Catchable fatal error: Object of class test could not be converted to string in, this method must return a string.
Before PHP 5.2.0, the __toString method could only take effect when used in conjunction with echo() or print(). After PHP 5.2.0, it can be used in any string environment (for example, through printf(), using the %s modifier), but cannot be used in non-string environments (such as using the %d modifier). From PHP 5.2.0, if an object that does not define the __toString method is converted to a string, an E_RECOVERABLE_ERROR error will be reported.
8. __sleep, __wakeup
 ◆__sleep Use
when serializing
 ◆__wakeup is called during deserialization
 serialize() checks whether there is a function with the magic name __sleep in the class. If so, the function will run before any serialization. It clears the object and should return an array containing the names of all variables in the object that should be serialized.
The purpose of using __sleep is to close any database connections the object may have, submit pending data, or perform similar cleanup tasks. Additionally, this function is useful if you have very large objects that do not need to be stored completely.
In contrast, unserialize() checks for the existence of a function with the magic name __wakeup. This function can reconstruct any resources the object may have, if present. The purpose of using __wakeup is to reestablish any database connections that may have been lost during serialization and to handle other reinitialization tasks.
9. __set_state
This static method will be called when var_export() is called (valid since PHP 5.1.0).
The only parameter of this method is an array, which contains class properties arranged in the format of array('property' => value, ...).
10. __invoke (valid for PHP 5.3.0 or above)
When trying to call an object by calling a function, the __invoke method is automatically called.
11. __callStatic (valid for PHP 5.3.0 or above)
It works similar to the __call() magic method, __callStatic() is to handle static method calls.
PHP does tighten the definition of the __callStatic() method; it must be public and must be declared static. Likewise, the __call() magic method must be defined as public, as must all other magic methods

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477768.htmlTechArticleFrom PHP 5 and later, classes in PHP can use magic methods. It stipulates that methods starting with two underscores (__) are reserved as magic methods, so it is recommended that you do not use function names...
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
php5和php8有什么区别php5和php8有什么区别Sep 25, 2023 pm 01:34 PM

php5和php8的区别在性能、语言结构、类型系统、错误处理、异步编程、标准库函数和安全性等方面。详细介绍:1、性能提升,PHP8相对于PHP5来说在性能方面有了巨大的提升,PHP8引入了JIT编译器,可以对一些高频执行的代码进行编译和优化,从而提高运行速度;2、语言结构改进,PHP8引入了一些新的语言结构和功能,PHP8支持命名参数,允许开发者通过参数名而不是参数顺序等等。

演示win7调整屏幕亮度的方法演示win7调整屏幕亮度的方法Jul 08, 2023 pm 07:49 PM

不同的电脑系统在调整屏幕亮度的操作方法上会有些不同,最近就有使用win7系统的网友不知道win7怎么调整屏幕亮度,看久了电脑眼睛比较酸痛。下面小编就教下大家win7调整屏幕亮度的方法。具体的操作步骤如下:1、点击win7电脑左下角的“开始”,在弹出的开始菜单中选择“控制面板”打开。2、在打开的控制面板中找到“电源选项”打开。3、也可以用鼠标右键电脑右下角的电源图标,在弹出的菜单中,点击“调整屏幕亮度”,如下图所示。两种方法都可以用。4、在打开的电源选项窗口的最下面可以看到屏幕亮度调整的滚动条,直

win10监控摄像头打开照片的方法win10监控摄像头打开照片的方法Jul 10, 2023 pm 09:41 PM

如果我们手头没有手机,只有电脑,但我们必须拍照,我们可以使用电脑内置的监控摄像头拍照,那么如何打开win10监控摄像头,事实上,我们只需要下载一个相机应用程序。打开win10监控摄像头的具体方法。win10监控摄像头打开照片的方法:1.首先,盘快捷键Win+i打开设置。2.打开后,进入个人隐私设置。3.然后在相机手机权限下打开访问限制。4.打开后,您只需打开相机应用软件。(如果没有,可以去微软店下载一个)5.打开后,如果计算机内置监控摄像头或组装了外部监控摄像头,则可以拍照。(因为人们没有安装摄

人形机器人会变魔术了,春晚节目组了解一下人形机器人会变魔术了,春晚节目组了解一下Feb 04, 2024 am 09:03 AM

一眨眼的功夫,机器人都已经学会变魔术了?只见它先是拿起桌上的水勺,向观众证明了里面什么也没有……然后,它又把手中鸡蛋似的物体放了进去,然后把水勺放回桌子上,开始“施法”……就在它把水勺再次拿起的时候,奇迹发生了。原先放进去的鸡蛋不翼而飞,跳出的东西变成了一个篮球……再来看一遍连贯动作:△此动图为二倍速一套动作下来如行云流水,只有把视频用0.5倍速反复观看,才终于发现了其中的端倪:如果手速再快一些,大概真的就可以瞒天过海了。有网友感叹,机器人变魔术的水平比自己还要高:为我们表演这段魔术的,是Mag

基于Java的机器视觉实践和方法介绍基于Java的机器视觉实践和方法介绍Jun 18, 2023 am 11:21 AM

随着科技的不断发展,机器视觉技术在各个领域得到了广泛应用,如工业自动化、医疗诊断、安防监控等。Java作为一种流行的编程语言,其在机器视觉领域也有着重要的应用。本文将介绍基于Java的机器视觉实践和相关方法。一、Java在机器视觉中的应用Java作为一种跨平台的编程语言,具有跨操作系统、易于维护、高度可扩展等优点,对于机器视觉的应用具有一定的优越性。Java

php5如何改80端口php5如何改80端口Jul 24, 2023 pm 04:57 PM

php5改80端口的方法:1、编辑Apache服务器的配置文件中的端口号;2、辑PHP的配置文件以确保PHP在新端口上工作;3、重启Apache服务器,PHP应用程序将开始在新的端口上运行。

win7怎么调屏幕亮度的两种简单方法win7怎么调屏幕亮度的两种简单方法Jul 08, 2023 pm 06:33 PM

目前有很多屏幕亮度调整软件,我们可以通过使用软件进行快速调整或者通过显示器上自带的亮度功能进行调整。以下是详细的Win7屏幕亮度调整方式,您可以通过教程中的方法进行快速调整即可。Win7系统电脑怎么调节屏幕亮度教程:1、依次点击“计算机—右键—控制面板”,如果没有也可以在搜索框中进行搜索。2、点击控制面板下的“硬件和声音”,或者点击“外观和个性化”都可以。3、点击“NVIDIA控制面板”,有些显卡可能是AMD或者Intel的,请根据实际情况选择。4、调节图示中亮度滑块即可。5、还有一种方法,就是

PHP文件下载方法及常见问题解答PHP文件下载方法及常见问题解答Jun 09, 2023 pm 12:37 PM

PHP是一个广泛使用的服务器端编程语言,它的许多功能和特性可以将其用于各种任务,包括文件下载。在本文中,我们将了解如何使用PHP创建文件下载脚本,并解决文件下载过程中可能出现的常见问题。一、文件下载方法要在PHP中下载文件,我们需要创建一个PHP脚本。让我们看一下如何实现这一点。创建下载文件的链接通过HTML或PHP在页面上创建一个链接,让用户能够下载文件。

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools