Magic functions in PHP include tostring, call, clone, and autoload. I hope this tutorial will be helpful to all my friends.
Summary of object-oriented development
1. Description and configuration of objects. (can be understood as the output text description of the object)
2. A processing of object method. (It can be understood as customizing a more humane error message when an exception occurs)
3. Application of cloned objects. (It can be understood as cloning another object that is the same as the original object. Note that it is another one.)
4. Method to automatically load objects. (Can be understood as a quote)
1. Object description and configuration
Method name: __tostring()
Format:
The code is as follows | Copy code | ||||||||
class My {
return "Write a text description of this class here"; //If you use return, echo will make an error. }
$p = new My(); echo $p; |
The code is as follows | Copy code | ||||||||||||||||||||||||||||||||||||||||||||||||
class My { function __toString() { return "The purpose of this class is to protect the earth."; }
$p = new My(); echo $p;
2. Exception handling of object methods Exception handling when calling some non-existent object methods allows the program to run normally.
"; //This is the cloned and modified object. If $b = $p is used, then modify $b-> ;name="Little Pig Head";, and then output $p, it will also be modified. echo $p->name; //This is the original object, which has been cloned by $b. Everything else is the same, but they are independent. Format:
Previous article:PHP Programmer Written Test and Interview Questions Sharing_PHP Tutorial
Next article:PHP controls page browser cache and enables gzip transmission_PHP tutorial
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Group MySQL results by ID for looping over
I have a table with flight data in mysql. I'm writing a php code that will group and displ...
From 2024-04-06 17:27:56
0
1
406
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|