PHP object-oriented development-magic functions-PHP Tutorial

Source: Internet
Author: User
PHP object-oriented development Magic functions. In php, Magic functions include tostring, call, clone, and autoload. I hope this tutorial will be helpful to all of you. Summary of object-oriented development 1. Object Description and configuration of magic functions in php include tostring, call, clone, and autoload. I hope this tutorial will be helpful to all of you.

Object-Oriented Development Summary

1. description and configuration of the object. (It can be understood as the output text description of the object)
2. processing an object method. (It can be understood that, when an exception occurs, a more user-friendly error message is customized)
3. application of the cloned object. (It can be understood that, on the original object, the same object is cloned, and the other object is the same .)
4. Methods for automatically loading objects. (Can be understood as reference)

1. Object Description and configuration

Method name __tostring ()
Format:

The code is as follows:
Class My {
Function _ tostring (){
Return "write the text description of this class here"; // If you want to use return, echo will fail.
}
}
$ P = new My ();
Echo $ p;

Instance:

The code is as follows:
Class My {
Function _ toString (){
Return "is used to protect the Earth. ";
}
}
$ P = new My ();
Echo $ p;
?>

2. exception handling of object methods

Exception handling for calling some non-existing object methods is that the program runs normally.
Method name: __ call ($ funname, $ arr_value)
Format:

The code is as follows:
Class My {
Function _ call ($ n, $ v ){
Echo "incorrect method name:". $ n;
Echo "incorrect parameter:". $ v;
}
}

Instance:

The code is as follows:

Class My {
Function _ toString (){
Return "is used to protect the Earth. ";
}

Function _ call ($ n, $ v ){
Echo "error method". $ N ."
";
Echo "error value". Print_r ($ v ).""; // The value is transmitted as an array, so print_r is used.
}
}
$ P = new My ();
$ P-> demo ("first", 6 );
?>

3. object cloning

By cloning, you can generate two identical objects in the memory or upgrade the original object. (Not a simple assignment, but two memory blocks are opened in the memory. the cloned and cloned two objects are the same as the two property methods)
Method name: __ clone ()
Keyword: clone
Format:

The code is as follows:
Class My {
Function _ clone (){
Echo "functions automatically called during cloning ";
}
}
$ A = new My ();
$ B = clone $;


Instance:

The code is as follows:

Class My {
Public $ name = "";
Function _ toString (){
Return "is used to protect the Earth. ";
}

Function _ call ($ n, $ v ){
Echo "error method". $ N ."
";
Echo "error value". Print_r ($ v ).""; // The value is transmitted as an array, so print_r is used.
}
}
$ P = new My ();
$ B = clone $ p;

Echo $ B-> name = "Pig "."
"; // This is the cloned and modified object. if $ B = $ p is used, modify $ B-> name =" "; and then output $ p, it will also be modified.
Echo $ p-> name; // This is the original object. it has been cloned by $ B. Others are the same, but they are independent.
?>


4. Methods for automatically loading objects


Get the object name quickly and load it into the current page automatically
Method name __autoload ()
Format:

The code is as follows:
Function _ autoload ($ class_n ){
Include ($ class_n. ". php ");
}
$ P = new MyPc (); // The Automatic Call is executed as follows
$ D = new demo (); // include ("mypc. php ")

Instance:

The code is as follows:

Function _ aotoload ($ class_name ){
Include ($ class_name. ". php"); // write it outside the class.
}

$ P = new class_name (); // the class name instantiated here is replaced by the above function variable $ class_name, which is very convenient.
?>

Bytes. Object-Oriented Development Summary 1. Object Description and configuration...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.