Get the App
SLTechnology News&Howtos  ›  Development  › 

What magic methods does PHP have?

Shulou Source: shulou.com Published: 2022-06-02 07:42:42 10月04日 Update

This article mainly shows you "what magic methods PHP has", the content is simple and clear, and I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "what magic methods does PHP have?"

PHP is a very good scripting language, and other programming languages have a very different place, that is magic, PHP has a lot of magic methods to achieve some very incredible functions.

What is the magic method of PHP?

When defining a class, methods that begin with two underscore characters (_ _) are magic methods, and the method names are predefined by PHP, each with magic functionality. When defining methods ourselves, it is not recommended to start with two underscore characters, which will be confused with PHP's magic method.

What magic tricks does PHP have?

PHP provides many magic methods, such as _ _ construct (), _ _ destruct (), _ _ call (), _ _ callStatic (), _ _ get (), _ _ set (), _ _ isset (), _ _ unset (), _ _ sleep (), _ _ wakeup (), _ _ toString (), _ _ invoke (), _ _ set_state (), _ _ clone () and so on. Today I'm going to tell you about five of them that are very practical.

1: _ _ construct () construction method

Timing of execution: executes automatically when an object is instantiated using a class using the new keyword

Function: usually used to initialize the properties of an object

Usage example:

How to remember: construct meaning: composition; construction; construction; pronunciation: [k construct n str construct kt], which can be understood as a method of constructing an object, which is automatically executed when the object is constructed.

2: _ _ destruct () destructing method

Timing of execution: automatically executes when the object is destroyed (unset or PHP execution ends)

Functional role: usually used to release third-party resources occupied by objects (such as databases)

Usage example:

How to remember: destruct meaning: destroy; destroy; pronunciation: [d destroy kt]

3: _ _ toString () converts the string

Timing of execution: executes automatically when the object is used as a string

Function: compatible with the string invocation form of the object, showing the string shape of the object according to the needs of the user

Usage example:

How to remember: toString meaning: to to; toward, toward; String string; pronunciation: [tu, tweak] [str orientation]

4: _ _ invoke () function call method

Timing of execution: automatically executes when an object is used as a function

Function function: a function call form of a compatible object that allows the user to use the object in the same way as a function

Usage example:

How to remember: invoke meaning: quote; beg spirit; pray; pronunciation: ["n" vo "k]

5: _ _ clone () cloning method

Timing of execution: automatically executes when cloning (clone) objects

Function: when you clone an object, you can perform some other operations at the same time, and even prevent the object from being cloned

Usage example:

How to remember: clone meaning: copy; clone; pronunciation: [klo pronunciation]

These are all the contents of this article entitled "what Magic methods does PHP have?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Method object magic function character function meaning string timing example pronunciation function content article underscore two beginning form user programming language Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS vpn Microsoft Huawei Apple