In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces what is the magic method in php, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.
Magic methods in the class
PHP magic methods are built-in functions that are called automatically at some point, starting with two consecutive underscores.
Magic method _ _ construct () in class
Class, which initializes the object and runs automatically when the object is instantiated
_ _ destruct ()
Destructor, which is used to free up the memory occupied by the object when the php is terminated. The destructor is the garbage collection mechanism of php, using stack structure, last-in, first-out.
Examples of constructors and destructors class computer {private $brand; function _ construct ($brand) {$this- > brand = $brand;} function _ destruct () {echo "release". $this- > brand. "
";} $myComputer = new computer (" MAC "); $yourComputer = new computer (" Asus "); $hisComputer = new computer (" Dell "); echo" end of php file "
"
The output is as follows
End of php filerelease Dellrelease Asusrelease MAC
You can find that the destructor does not execute until the end of the execution of the php file
_ _ get ($name)
Member properties or methods defined with the protected and private keywords in a class are not accessible through an instance of the object. The _ _ get () method executes automatically and only when an instance of the object accesses the proctected and private member properties (not when accessing member methods because it doesn't make sense).
The significance of the _ _ get () method is to process the proctected and private member attributes and output them.
_ _ get () has one and only one input parameter
An example of the _ _ get () method is class computer {private $brand; protected $owner; public $price; function _ _ construct ($brand, $owner, $price) {$this- > brand = $brand; $this- > owner = $owner; $this- > price = $price;} function _ _ get ($name) {echo "It's up to me to decide if let you konw the owner and the brand of this computer or not:)
"; echo" I will tell you the name of woner: ". $this- > owner."
"; echo" I won't tell you that the brand is ".md5 ($this- > brand)
"; echo"
";} function _ destruct () {echo" release ". $this- > brand."
";} $myComputer = new computer (" MAC "," me "," 1000 "); $yourComputer = new computer (" Asus "," you "," 1000 "); $hisComputer = new computer (" Dell "," his "," 700 "); echo $myComputer- > price; echo"
"; echo $myComputer- > owner;echo $yourComputer- > brand;echo" end of php file
"
The output is as follows
1000It's up to me to decide if let you konw the owner and the brand of this computer or not:) I will tell you the name of woner: meI won't tell you that the brand is 2e25c285356cbb0ed8785a1377027d79It's up to me to decide if let you konw the owner and the brand of this computer or not:) I will tell you the name of woner: youI won't tell you that the brand is cb6ab3315634a1e4d11b091ba48b60baend of php filerelease Dellrelease Asusrelease MAC
As you can see, the _ _ get () method is not called when accessing the public member property price. When we output the brand, we use md5 to encrypt it, and the use of the output after processing the encapsulated member attributes is the meaning of the get method.
_ _ set ($name, $value)
_ _ set ($name, $value) is used to reassign or define methods or properties encapsulated in the current class.
Similar to but different from get, _ _ set ($name, $value) executes automatically when a member property is accessed, where $name is the name of the accessed member property and $value is the value assigned to the member property
An example of _ _ set () class computer {private $brand; protected $owner; function _ _ construct ($brand, $owner, $price) {$this- > brand = $brand; $this- > owner = $owner; $this- > price = $price;} function _ _ get ($name) {echo "It's up to me to decide if let you konw the owner and the brand of this computer or not:)
"; echo" I will tell you the name of woner: ". $this- > owner."
"; echo" I won't tell you that the brand is ".md5 ($this- > brand)
"; echo"
";} function _ set ($name, $value) {$this- > owner = $value; echo" set $name to $value ".
";} function _ destruct () {echo" release ". $this- > brand."
";} $myComputer = new computer (" MAC "," me "," 1000 "); echo $myComputer- > owner =" my friend "; echo $myComputer- > owner;echo" end of php file
"
Output result
Set owner to my friendmy friendIt's up to me to decide if let you konw the owner and the brand of this computer or not:) I will tell you the name of woner: my friendI won't tell you that the brand is 2e25c285356cbb0ed8785a1377027d79end of php filerelease MAC
We see that set is called when assigning a value to owner and get is called when accessing the property.
_ _ tostring ()
Used to print the object handle directly, that is, when we use echo to add the object name, _ _ torsring () will be called automatically
_ _ tosring () example class computer {function _ tostring () {return "This is a computer class";}} $myComputer = new computer (); echo $myComputer
If there is no _ _ totring () method, we cannot use the echo+ object name, and fatal error will appear.
_ _ call ($method, $arguments)
When we call a method that does not exist, _ _ call () executes automatically for exception handling and keeps the program running normally.
_ _ call () example class computer {function start () {echo "starting computer
";} function _ call ($m, $a) {echo" erro function: ". $m; echo"
"; echo" error param: "; print_r ($a); echo"
";} $myComputer = new computer (); $myComputer- > start (); $myComputer- > shutdown ('10 min','20 min'); echo" here "
The output is
Starting computererro function: shutdownerror param: Array ([0] = > 10 min [1] = > 20 min) here
We can see that $method returns the wrong function name, while arguments returns parameters, and finally outputs "here" indicating that the program continues to run normally.
_ _ clone () method and clone keyword
The clone keyword is used to copy an object, and the _ _ clone () method is a function that is called automatically when the object is cloned.
Clone example class computer {public $name; function _ _ clone () {echo "A computer has been cloned
";} $myComputer = new computer (); $youComputer = $myComputer;$youComputer- > name = 'pc1';echo" My computer's name is ". $myComputer- > name."
"; echo"
"; $hisComputer = clone $myComputer;$hisComputer- > name = 'pc2';echo" My computer's name is. $myComputer- > name. "
; echo "His computer's name is". $hisComputer- > name. "
"
Output result
My computer's name is pc1A computer has been clonedMy computer's name is pc1His computer's name is pc2
We see that using the = sign does not copy the object, it just adds an alias to the object, where $myComputer and $youComputer point to the same piece of memory, changing the value of $youComputer is equivalent to changing the value of $myComputer.
_ _ autolaod ()
When an object is instantiated, _ _ autolaod () is automatically called to quickly get the corresponding class file.
_ _ autoload () example
Examples of exception handling with try and catch
Function _ autoload ($class_name) {echo "want toload". $class_name. "
"; if (file_exists ($class_name." .class.php ") {include ($class_name." .class.php ");} else {throw new Exception (" Unable to laod ". $class_name." .class.php ");} try {$obj = new myClass ();} catch (Exception $e) {echo $e-> getMessage ()."
Thank you for reading this article carefully. I hope the article "what are the Magic methods in php" shared by the editor will be helpful to you? at the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.