About PHP __call and __callstatic content

Source: Internet
Author: User
PHP 5.3 Adds the __call and __callstatic magic methods.

__call The __call method is called automatically when the method to invoke does not exist or has insufficient permissions.

__callstatic The __callstatic method is called automatically when a static method that is called does not exist or has insufficient permissions.

__call ($funcname, $arguments)

__callstatic ($funcname, $arguments)

Parameter description:

$funcname String called by the method name.

$arguments The parameter that the Array takes when calling the method.

__call Example

<?phpclass 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 show () {if ($this->memberdata) {f Oreach ($this->memberdata as $key = $member) {echo $key. ': ' $member. '            <br> '; }}}}class profession extends member{public function show () {if ($this->memberdata) {fo Reach ($this->memberdata as $key = + $member) {echo $key. ': ' $member. ' <br> '; }}}} $userobj = new User (); $userobj->set_name (' Fdipzone '); $userobj->set_age; $userobj->show (); $prob j = new Profession (); $probj->set_profession (' IT SERVICE '); $probj->set_price (2500); $probj->show ();? >

__callstatic example

<?phpclass member{protected static $memberdata = Array ();                public static function __callstatic ($func, $arguments) {list ($type, $name) = Explode (' _ ', $func);        if (!in_array ($type, Array (' Set ', ' Get ')) | | $name = = ') {return ';        } $feature = Get_called_class ();                Switch ($type) {case ' set ': Self:: $memberdata [$feature] [$name] = $arguments [0];            Break Case ' get ': return Isset (self:: $memberdata [$feature] [$name])?                Self:: $memberdata [$feature] [$name]: ';            Break Default:}}}class User extends member{public static function show () {$feature = Get_called_class        (); if (self:: $memberdata [$feature]) {foreach:: $memberdata [$feature] as $key + = $member) {echo $key. ': ' $member. '            <br> '; }}}}class profession extends member{public static function show () {$feature = Get_called_class (); if (self:: $memberdata [$feature]) {foreach:: $memberdata [$feature] as $key + = $member) {echo $key. ': ' $member. '            <br> '; }}}}user::set_name (' Fdipzone '); User::set_age (29); User::show (); Profession::set_profession (' IT SERVICE '); Profession::set_price (2500); Profession::show ();? >

This article explains about PHP __call and __callstatic content, more relevant knowledge, please pay attention to the PHP Chinese web.

Related recommendations:

About memcached common commands and instructions for use

About phpmailer-php e-mail Transport class

Understanding of PHP Traversal folders and file classes and processing classes

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.