In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to use the magic methods of 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!
In PHP, methods named with a double underscore (_ _) are called magic methods in PHP, and they play an important role in PHP. Magic methods include:
The method name describes the constructor of the _ _ construct () class _ _ destruct () class destructor _ _ call ($funName, $arguments) when the _ _ call () method is called when an undefined or unreachable method is called. _ _ callStatic ($funName, $arguments) the _ _ callStatic () method is called when an undefined or unreachable static method is called. _ _ get ($propertyName) the _ _ get () method is called when you get a member variable of a class. _ _ set ($property, $value) the _ _ set () method is called when a member variable of a class is assigned. _ _ isset ($content) the _ _ isset () method is called when isset () or empty () is called to assign a value to an undefined or unreachable member. _ _ unset ($content) the _ _ unset () method is called when reset () is called to update an undefined or unreachable member. _ _ sleep () when serializing serialize (), the _ _ sleep () method is called first. _ _ wakeup () when deserializing deserialization (), the _ _ wakeup () method is called first. _ _ toString () when the display object is output directly using the echo method, the _ _ toString () method is called first. When _ _ invoke () accesses an object using the calling function (function), the _ _ invoke () method is called first. _ _ set_state ($an_array) when the var_export () method is called, the _ _ set_state () method is called. _ _ clone () the _ _ clone () method is called when the object is copied and assigned. Called when _ _ autoload ($className) attempts to load an undefined class. _ _ debugInfo () outputs debug information.
This article will use some examples to demonstrate the use of PHP magic methods.
1.__construct ()
When an object is created, the constructor of the PHP class is the first method to be called. Every class has a constructor. If you do not explicitly declare and define it in the class, there will be a default no-parameter class constructor, although it will not appear in the class definition.
1) the application of construction method
Class constructors are typically used to perform initialization tasks, such as initializing assignments to members when an object is created.
2) declaration format of constructor in class
Function _ _ constrct ([parameter list]) {the concrete implementation of the method / / usually assigns initial values to member variables. }
Note: only one constructor can be declared in most classes. Because PHP does not support constructor overloading.
Here is a complete example:
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.