Php magic method _ call () ,__ clone () ,__ set () ,__ get () _ PHP Tutorial

Source: Internet
Author: User
The magic method _ call () ,__ clone () ,__ set () ,__ get () in php (). Magic methods in php _ call () ,__ clone () ,__ set () ,__ get () _ call ($ a, $ v ), when the external call class does not exist or is invisible (using protected, private modification), the magic method _ call () ,__ clone () in php is automatically called (), __set () ,__ get ()
_ Call ($ a, $ v): when a method that does not exist or is invisible in an external call class of the class (modified using protected or private, the _ call (method name, array parameter) is automatically called. two parameters are passed to the modified method. The first parameter is the name of the called method, the second is to put the passed parameters into the array. The _ clone () method is triggered when the class object is cloned. _ Set ($ key, $ val) triggers this method when the class does not exist or is invisible (protected, private modifier) in the external setting class of the class. at this time, two parameters are passed, the first is the attribute name, and the second is the attribute value. _ Get ($ key) triggers this method when the class does not exist or is invisible (protected, private modifier) in the external get class. at this time, one parameter is passed, that is, the attribute name. Example:

 ";} Public function _ call ($ name, $ arg) {echo" someone wants to call a nonexistent or invisible method name ", $ name ,", the first parameter is $ arg [0].
";} Public function _ set ($ k, $ v) {echo" someone wants to set a property that does not exist or is invisible ", $ k," the value is ", $ v ,"
";}Public function _ get ($ k) {echo" someone wants to get a nonexistent or invisible property ", $ k ;}}

$ Test1 = new test ();

$ Test3 = $ test1; // at this time, $ test3 and $ test1 are the same object, that is, they all point to the same object.

$ Test2 = clone $ test1; // after cloning, $ test1 and $ test2 are two different objects, although the stored values are the same

$ Test1-> abc (1, 2, 3 );

$ Test1-> a = 3;

$ Test1-> B;

Round () ,__ clone () ,__ set () ,__ get () _ call ($ a, $ v ), when the method does not exist or is invisible in the external call class of the class (modified with protected and private), it is automatically called...

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.