PHP object-oriented programming-special practices DAY4

Source: Internet
Author: User
: This article mainly introduces PHP object-oriented programming-special practice DAY4. if you are interested in PHP tutorials, refer to it. Oriented ObjectSpecial practices
(Only available in PHP, applicable in other languages ObjectNo)
Oriented Object-- Magic method
_ Construct (), _ destruct () constructor and Destructor
_ Tostring ()
_ Invoke ()
_ Call (), _ callStatic ()
_ Get (), _ set (), _ isset (), _ unset ()
_ Clone ()
_ Tostring ()
When ObjectThis method is automatically called when it is used as a String.
Echo $ obj;
_ Invoke ()
When ObjectThis method is automatically called when it is called as a method.

$ Obj (4 );

 ObjectPublic function _ tostring () {return "This is the Class MagicTest.";} // _ invokeObjectWhen called as a method, the public function _ invoke ($ x) {echo "_ invoke called with parameter" is automatically called ". $ x. "\ n" ;}}$ obj = new MagicTest (); echo $ obj. "\ n"; $ obj (5);?>

Output:
This is the Class MagicTest.
_ Invoke called with parameter 5

_ Call ()
WhenObjectWhen you access a method name that does not exist, the __call () method is automatically called.
_ CallStatic ()
WhenObjectAccess does not existStatic methodWhen the name is specified, the __callstatic () method is automatically called.
These two methods are also used as overloading methods in PHP)
Note overwrite)
Through these two methods, the call of the same method name can correspond to different method implementations

 Static methodThis method must be set to staticpublic static function _ callStatic ($ name, $ arguments) {echo "Static Calling ". $ name. "with parameters :". implode (",", $ arguments ). "\ n" ;}}$ obj = new MagicTest (); // The names of the runTest methods cannot be identical, but the magic method is used, you can call $ obj-> runTest ("para1", "para2"); // The runTest method is not declared, because the _ call magic method exists, you can also call MagicTest: runTest ("para1", "para2"); // The runTest method is not declared, because the magic method of _ callStatic exists, can also be called?>

Output:
Calling runTest with parameters: para1, para2
Static Calling runTest with parameters: para1, para2


_ Get (), _ set (), _ isset (), _ unset ()
When assigning values to inaccessible properties, __set () will be called
When you read the value of an inaccessible attribute, __get () will be called
When isset () or empty () is called for an inaccessible attribute, __isset () is called.
When unset () is called for an inaccessible attribute, __unset () is called.
The so-called inaccessible attribute is actually called to find that this attribute is not defined. at this time, different operations will trigger different magic methods.
These methods are also called the Magic Methods of attribute overloading.


 Static methodThis method must be set to staticpublic static function _ callStatic ($ name, $ arguments) {echo "Static Calling ". $ name. "with parameters :". implode (",", $ arguments ). "\ n";} // the property is overloaded with public function _ get ($ name) {return "Getting the property ". $ name. "\ n";} public function _ set ($ name, $ value) {echo "Setting the property ". $ name. "to value ". $ value. "\ n";} public function _ isset ($ name) {echo "_ isset invoked \ n"; return t Rue;} public function _ unset ($ name) {echo "unsetting property ". $ name. "\ n" ;}}$ obj = new MagicTest (); echo $ obj-> className. "\ n"; // className is not defined, but the magic method _ get is used. this method seems to have been defined. $ obj-> className = 'magicclasss '; // define the className name as MagicClassXecho '$ obj-> name is set? '. Isset ($ obj-> className). "\ n"; echo' $ obj-> name is empty? '. Empty ($ obj-> className). "\ n"; unset ($ obj-> className);?>

Output:
Getting the property className
Setting the property className to value MagicClassX
_ Isset invoked
$ Obj-> name is set? 1 // if return is true, the result is 1. if return is false, the result is empty.
_ Isset invoked
$ Obj-> name is empty? // If return is true, the result is blank. if return is false, the result is 1.
Unsetting property className

OrientedObject-- Magic method
_ Clone ()
ObjectProgramming-special practice DAY 4 ">

 Name = 'TBD'; // block the data you do not want to copy, block the data, and set its initial value.} $ james = new NbaPlayer (); $ james-> name = 'James '; echo $ James-> name. "\ n"; $ james2 = clone $ james; echo "Before set up: James2's :". $ james2-> name. "\ n"; $ james2-> name = 'james2'; echo "James's :". $ james-> name; echo "James's :". $ james2-> name;?>

Output:
James
Before set up: James2's: TBD
James's: James
James's: James 2 // James2 data will not affect James's data
------------------------------------------------------------------------------- PHP oriented ObjectProgramming Notes are complete ---------------------------------------------------------------------------------------

The above introduces PHP object-oriented programming-special practice DAY 4, including some content, hope to be helpful to friends who are interested in PHP tutorials.

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.