Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Magic Methods in php

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to understand Magic Methods in php". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

One advantage of PHP objects is that you can use magic methods that override the default behavior of a class without modifying external code, which makes the PHP syntax less redundant and more extensible. These methods are easy to identify, and they all start with a double underscore (_).

For example, _ _ construct (), _ _ destruct (), _ _ call (), _ _ callStatic (), _ _ get (), _ _ set (), _ _ isset (), _ _ unset (), _ _ sleep (), _ _ wakeup (), _ _ toString (), _ _ invoke (), _ _ set_state () and _ _ clone () are called "magic methods" (Magic methods) in PHP. You cannot use these method names when naming your own class methods unless you want to use their magic capabilities.

Note:

PHP keeps all class methods that start with _ _ (two underscores) as magic methods. Therefore, when defining class methods, in addition to the above magic methods, it is not recommended to prefix them with _ _.

1 、 _ _ get 、 _ _ set

These two methods are designed for properties that are not declared in the class and their parent class.

The copy code is as follows:

_ _ get (string $name) / / access this method when calling an undefined property

Called when _ set (string $name, mixed $value) / / assigns a value to an undefined attribute

The non-declaration here includes attributes whose access control is proteced,private (that is, properties that do not have permission to access) when using object calls.

2 、 _ _ isset 、 _ _ unset

The copy code is as follows:

_ _ isset ($property) / / call this method when the isset () function is called on an undefined property

_ _ unset ($property) / / call this method when the unset () function is called on an undefined property

3 、 _ _ call 、 _ _ callStatic

The copy code is as follows:

_ _ call (string $name, array $arguments) / / this method is called when an undefined method is called.

The undefined methods here include methods that do not have permission to access.

The copy code is as follows:

_ _ callStatic (string $name, array $arguments)

When an inaccessible method (such as undefined or invisible) is called in a static method, _ _ callStatic () is called.

_ _ callStatic works like the _ _ call () magic method, _ _ callStatic () is used to handle static method calls, and PHP5.3.0 and above are valid.

PHP does enhance the definition of the _ _ callStatic () method; it must be public and must be declared static. Similarly, the _ _ call () magic method must be defined as public, as must all other magic methods.

4 、 _ _ autoload

The _ _ autoload function, which is called automatically when you try to use a class that has not yet been defined. By calling this function, the script engine has one last chance to load the required classes before the PHP error fails.

Note: the exception thrown in the _ _ autoload function cannot be caught by the catch statement block and cause a fatal error.

5 、 _ _ construct 、 _ _ destruct

The _ _ construct constructor, which is called when an object is created.

The advantage of using this method is that you can give the constructor a unique name, regardless of the name of the class it is in. In this way, when you change the name of the class, you do not need to change the name of the constructor.

The _ _ destruct destructor method, which PHP will call before the object is destroyed (that is, before it is cleared from memory).

By default, PHP simply frees the memory occupied by the object's properties and destroys the resources associated with the object.

Destructors allow you to execute arbitrary code to clear memory after using an object.

When PHP decides that your script is no longer related to objects, the destructor will be called.

Within the namespace of a function, this happens when the function return.

For global variables, this occurs at the end of the script. If you want to destroy an object explicitly, you can assign any other value to the variable pointing to the object, usually assigning the variable to NULL or calling unset.

6 、 _ _ clone

The object assignment in PHP5 is the reference assignment used. 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 you need to perform some initialization operations during object replication, you can do so in the _ _ clone method.

7 、 _ _ toString

The copy code is as follows:

Public string _ toString (void)

The _ _ toString method is called automatically when an object is converted to a string, such as when printing an object using echo. What should be shown. This method must return a string or it will issue a fatal error at the E_RECOVERABLE_ERROR level.

The copy code is as follows:

$myObject = new myClass ()

Echo $myObject

/ / Will look for a magic method echo

$myObject- > _ _ toString ()

Note: you cannot throw an exception in the _ _ toString () method. Doing so can lead to a fatal mistake.

It is important to note that before PHP 5.2.0, the _ _ toString () method took effect only when used directly with echo or print. After PHP 5.2.0, it can take effect in any string environment (for example, through printf (), using the% s modifier), but not in a non-string environment (such as using the% d modifier). As of PHP 5.2.0, converting an object without a _ _ toString () method to a string will result in an error at the E_RECOVERABLE_ERROR level.

8, _ _ sleep () and _ _ wakeup ()

The copy code is as follows:

Public array _ sleep (void)

Void _ wakeup (void)

The serialize () function checks to see if there is a magic method _ _ sleep () in the class. If it exists, the method is called before the serialization operation is performed. This feature can be used to clean up the object and return an array of all variable names that should be serialized in the object. If the method returns nothing, the NULL is serialized and an E_NOTICE-level error is generated.

Note:

_ _ sleep () cannot return the name of the private member of the parent class, which results in an E_NOTICE-level error. You can use the Serializable interface instead.

The _ _ sleep () method is often used to submit uncommitted data, or similar cleanup operations. At the same time, this feature is easy to use if you have some large objects, but you don't need to save them all.

In contrast, unserialize () checks to see if there is a _ _ wakeup () method. If so, the _ _ wakeup method is called first to prepare the resources needed by the object in advance.

_ _ wakeup () is often used in deserialization operations, such as re-establishing a database connection or performing other initialization operations.

9. _ _ invoke ()

The copy code is as follows:

Mixed _ _ invoke ([$...])

When you try to call an object as a function call, the _ _ invoke () method is called automatically.

10. _ _ set_state ()

The copy code is as follows:

Static object _ _ set_state (array $properties)

Since PHP 5.1.0, this static method will be called when var_export () is called to export the class.

The only parameter to this method is an array containing array ('property' = > value,...) The class properties of the format arrangement.

This is the end of "how to understand Magic Methods in php". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report