With the _ callstatic magic method of PHP 5.3, you can perform the magic by class name and method name at the same time.

Source: Internet
Author: User

It's hard to explain what you want to do. In short, with _ callstatic, you can save a lot.Code. In addition, this method can be called in the subclass. In combination with get_called_class, The subclass also performs magic.

Previously, when using the. NET Component Castle's activerecord,. Net version 2.0 can add a static getbyid method to each subclass. Now, PHP can also do this..


 

<? Phpclass activerecordbase {/** as of PHP 5.3.0 */public static function _ callstatic ($ name, $ arguments) {if ($ name = 'getbyid ') {$ id = $ arguments [0]; return get_called_class (). '('. $ id. ')';} Throw new exception ('invalid method :'. $ name) ;}} class person extends activerecordbase {}// output: Person (123) echo person: getbyid (123);?>

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.