PHP object model overload

Source: Internet
Author: User
PHP 4 already has an overloaded syntax to map external object models, just like Java and COM. PHP5 brings powerful object-oriented overloading, allowing programmers to establish custom actions to visit attributes and call methods. You can use the _ get and PHP4 overload syntax to create a ING of the external object model, just like Java and COM. PHP5 brings powerful object-oriented overloading, allowing programmers to establish custom actions to visit attributes and call methods.

You can use several special methods of _ get, _ set, and _ call for overloading. PHP will call these methods when the Zend Engine tries to visit a member and does not find them.

In the following example, __get and _ set replace all visits to the attribute variable array. if necessary, you can implement any type of filter you want. for example, the script can disable attribute value setting and use a certain prefix or contain a certain type of value at the beginning.

The _ call method clarifies how you call an undefined method. when you call an undefined method, the method name and parameters received by the method will be passed to the _ call method, and the value of _ call passed by PHP will be returned to the undefined method.

Listing1 User-level overloading

Class Overloader
{
Private $ properties = array ();

Function _ get ($ property_name)
{
If (isset ($ this-> properties [$ property_name])
{
Return ($ this-> properties [$ property_name]);
}
Else
{
Return (NULL );
}
}

Function _ set ($ property_name, $ value)
{
$ This-> properties [$ property_name] = $ value;
}

Function _ call ($ function_name, $ args)
{
Print ('invoking $ function_name () N ');
Print ('arguments :');
Print_r ($ args );

Return (TRUE );
}
}
$ O = new Overloader ();

// Invoke _ set () assigns a value to a non-existent attribute variable and activates _ set ()
$ O-> dynaProp = 'dynamic content ';

// Invoke _ get () activate _ get ()
Print ($ o-> dynaProp. 'N ');

// Invoke _ call () activate _ call ()
$ O-> dynaMethod ('Leon', 'zeev ');
?>

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.