PHP's __call and __callstatic magic method

Source: Internet
Author: User

PHP 5.3 added the __call and __callstatic magic method.

__call the __call method is automatically invoked when the method being invoked does not exist or has insufficient permissions.

__callstatic the __callstatic method is automatically invoked when the calling static method does not exist or if the permissions are insufficient.

__call ($funcname, $arguments)

__callstatic ($funcname, $arguments)

Parameter description:

The name of the method called $funcname String.

The parameter to take when the method is called $arguments Array.

__call Example

<?php class member{protected $memberdata = Array ();  
          
        Public Function __call ($func, $arguments) {list ($type, $name) = Explode (' _ ', $func);  
        if (!in_array ($type, Array (' Set ', ' Get ')) | | $name = = ') {return ';   
                Switch ($type) {case ' set ': $this->memberdata[$name] = $arguments [0];  
                  
            Break Case "Get": Return Isset ($this->memberdata[$name])?  
                $this->memberdata[$name]: ';  
                  
            Break Default:}} class User extends member{public function sho  
                W () {if ($this->memberdata) {foreach ($this->memberdata as $key => $member) { echo $key. ': $member. '  
            <br> '; {}}} class ProfeSsion extends member{public function show () {if ($this->memberdata) {foreach ($this ->memberdata as $key => $member) {echo $key. ': $member. '  
            <br> ';  
$userobj = new User ()}}}  
$userobj->set_name (' Fdipzone ');  
$userobj->set_age (29);  
      
$userobj->show ();  
$probj = new Profession ();  
$probj->set_profession (' IT SERVICE ');  
$probj->set_price (2500);  
      
$probj->show (); ?>

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/

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.