In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces what magic methods there are in php language. It is very detailed and has certain reference value. Friends who are interested must finish reading it.
Php magic methods include: "_ _ set ()", "_ _ get ()", "_ _ isset ()", "_ _ unset ()", "_ _ sleep ()", "_ _ wakeup ()", "_ _ call ()", "_ _ invoke ()", "_ _ set_state ()" and so on.
Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In object-oriented programming, PHP provides a series of magic methods, which provide a lot of convenience for programming, and play a very important role in PHP. Magic methods in PHP usually start with _ _ (two underscores) and do not need to be called explicitly but automatically under certain conditions.
The magic methods in PHP are shown in the following table:
The magic method automatically calls _ _ destruct () when it instantiates the class _ _ construct () class object automatically calls _ _ set () at the end of use, automatically calls _ _ get () when assigning values to undefined attributes, automatically calls _ _ isset () when it uses isset () or empty () function, automatically calls _ _ sleep () when using unset () to make Automatically call _ _ wakeup () when serializing with serialize _ _ wakeup () automatically call _ _ call () when using unserialize deserialization to call a method that does not exist call _ _ callStatic () automatically call _ _ toString () when calling a static method that does not exist automatically call _ _ invoke () when trying to call an object when a method is called automatically call _ _ set_state () when using var Called automatically when the _ export () function Accept an array parameter _ _ clone () automatically called when copying an object using clone _ _ debugInfo () when printing object information using var_dump ()
_ _ autoload ()
Try to load an undefined class
Example
Let's explain how to use these magic methods in the form of examples.
1. _ _ construct (), the constructor of the class
The constructor in php is the first method that is automatically called by the object after the object is created. There is a constructor in each class, and if it is not declared explicitly, there is a constructor with no parameters and empty content by default in the class.
1. The function of construction method.
Constructors are often used to perform useful initialization tasks, such as assigning initial values to member properties when creating an object.
2. the declaration format of the constructor in the class
Function _ _ constrct ([parameter list]) {method body / / is usually used to initialize the assignment of member attributes}
3. Things to pay attention to when declaring constructors in a class
1) only one constructor can be declared in the same class because PHP does not support constructor overloading.
2). The name of the constructor is _ _ construct () starting with two underscores.
Here is an example of it:
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.