PHP __call and __callstatic

Source: Internet
Author: User
Tags explode vars
<span id="Label3"></p><span class="link_title"><span class="link_title">php __call and __callstatic</span></span><p><p><span style="font-size: 14px;">PHP 5.3 Adds the <span style="color: #ff0000;">__call and <span style="color: #ff0000;">__callstatic Magic METHODS. </span></span></span></p></p><p><p><span style="font-size: 14px;"><span style="color: #ff0000;">__call the __call method is called automatically when the method to invoke does not exist or has insufficient permissions.</span></span></p></p><p><p><span style="font-size: 14px;"><span style="color: #ff0000;">__callstatic the __callstatic method is called automatically when a static method that is called does not exist or has insufficient permissions.</span></span></p></p><p><p><span style="font-size: 14px;"><br></span></p></p><p><p><span style="font-size: 14px;"><strong>__call ($funcname, $arguments)</strong></span></p></p><p><p><span style="font-size: 14px;"><strong>__callstatic ($funcname, $arguments)</strong></span></p></p><p><p><span style="font-size: 14px; color: #ff0000;"><strong><br>Parameter description:</strong></span></p></p><p><p><span style="font-size: 14px;"><strong>$funcname</strong> String called by the method Name.</span></p></p><p><p><span style="font-size: 14px;"><strong>$arguments</strong> The parameter that the Array takes when calling the Method.</span></p></p><p><p><br></p></p><p><p><strong><span style="font-size: 14px; color: #ff0000;">__call Example</span></strong></p></p><strong><strong>[php]</strong></strong>View Plaincopy <ol class="dp-c" start="1"> <li class="alt"><li class="alt"><?php</li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="keyword">Class member{</span></li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="keyword">protected <span class="vars">$memberdata = <span class="keyword">array (); </span></span></span></li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="keyword">Public <span class="keyword">function __call (<span class="vars">$func, <span class="vars">$arguments) {</span> </span> </span> </span></li></li> <li><li>List (<span class="vars">$type, <span class="vars">$name) = <span class="func">Explode (<span class="string">' _ ', <span class="vars">$func); </span></span></span></span></span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="keyword">if (!in_array (<span class="vars">$type, <span class="keyword">Array (<span class="string">' Set ',<span class="string">' Get ')) | | <span class="vars">$name = =<span class="string">") {</span> </span> </span> </span> </span> </span> </span></li></li> <li class="alt"><li class="alt"><span class="keyword">return <span class="string">'; </span></span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="keyword">switch (<span class="vars">$type) {</span> </span></li></li> <li class="alt"><li class="alt"><span class="keyword">case <span class="string">' Set ':</span> </span></li></li> <li><li><span class="vars">$this->memberdata[<span class="vars">$name] = <span class="vars">$arguments [0]; </span></span></span></li></li> <li class="alt"><li class="alt">break <span class="keyword">; </span></li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="keyword">case <span class="string">' Get ':</span> </span></li></li> <li><li><span class="keyword">return isset (<span class="vars">$this->memberdata[<span class="vars">$name])?  <span class="vars">$this->memberdata[<span class="vars">$name]: <span class="string">"; </span></span></span></span></span></span></li></li> <li class="alt"><li class="alt">break <span class="keyword">; </span></li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="keyword">default:</span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt"></li></li> <li><li>}</li></li> <li class="alt"><li class="alt"></li></li> <li><li>}</li></li> <li class="alt"><li class="alt"></li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="keyword">Class User <span class="keyword">extends member{</span></span></li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="keyword">public <span class="keyword">function show () {</span> </span></li></li> <li><li><span class="keyword">if (<span class="vars">$this->memberdata) {</span> </span></li></li> <li class="alt"><li class="alt"><span class="keyword">foreach (<span class="vars">$this->memberdata <span class="keyword">as <span class="vars">$key = +<span class="vars">$member) {</span> </span> </span> </span> </span></li></li> <li><li><span class="func">echo <span class="vars">$key.<span class="string">': '. <span class="vars"> $member.  <span class="string">' <br> '; </span></span></span></span></span></li></li> <li class="alt"><li class="alt">}</li></li> <li><li>}</li></li> <li class="alt"><li class="alt">}</li></li> <li><li></li></li> <li class="alt"><li class="alt">}</li></li> <li><li></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="keyword">Class profession <span class="keyword">extends member{</span></span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="keyword">public <span class="keyword">function show () {</span> </span></li></li> <li class="alt"><li class="alt"><span class="keyword">if (<span class="vars">$this->memberdata) {</span> </span></li></li> <li><li><span class="keyword">foreach (<span class="vars">$this->memberdata <span class="keyword">as <span class="vars">$key = +<span class="vars">$member) {</span> </span> </span> </span> </span></li></li> <li class="alt"><li class="alt"><span class="func">echo <span class="vars">$key.<span class="string">': '. <span class="vars"> $member.  <span class="string">' <br> '; </span></span></span></span></span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt">}</li></li> <li><li>}</li></li> <li class="alt"><li class="alt"></li></li> <li><li>}</li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="vars">$userobj = <span class="keyword">new User (); </span></span></li></li> <li class="alt"><li class="alt"><span class="vars">$userobj->set_name (<span class="string">' Fdipzone '); </span></span></li></li> <li><li><span class="vars">$userobj->set_age (29);</span></li></li> <li class="alt"><li class="alt"><span class="vars">$userobj->show ();</span></li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="vars">$probj = <span class="keyword">New Profession (); </span></span></li></li> <li><li><span class="vars">$probj->set_profession (<span class="string">' IT SERVICE '); </span></span></li></li> <li class="alt"><li class="alt"><span class="vars">$probj->set_price (2500);</span></li></li> <li><li><span class="vars">$probj->show ();</span></li></li> <li class="alt"><li class="alt"></li></li> <li><li>?></li></li> </ol><p><p><span style="font-size: 14px; color: #ff0000;"><strong>__callstatic Example</strong></span></p></p><strong><strong>[php]</strong></strong>View Plaincopy <ol class="dp-c" start="1"> <ol class="dp-c" start="1"> <li class="alt"><li class="alt"><?php</li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="keyword">Class member{</span></li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="keyword">protected <span class="keyword">static <span class="vars">$memberdata = <span class="keyword">array (); </span></span></span></span></li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="keyword">public <span class="keyword">static <span class="keyword">function __callstatic (<span class="vars">$func, <span class="vars">$arguments) {</span> </span> </span> </span> </span></li></li> <li><li></li></li> <li class="alt"><li class="alt">List (<span class="vars">$type, <span class="vars">$name) = <span class="func">Explode (<span class="string">' _ ', <span class="vars">$func); </span></span></span></span></span></li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="keyword">if (!in_array (<span class="vars">$type, <span class="keyword">Array (<span class="string">' Set ',<span class="string">' Get ')) | | <span class="vars">$name = =<span class="string">") {</span> </span> </span> </span> </span> </span> </span></li></li> <li><li><span class="keyword">return <span class="string">'; </span></span></li></li> <li class="alt"><li class="alt">}</li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="vars">$feature = Get_called_class (); </span></li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="keyword">switch (<span class="vars">$type) {</span> </span></li></li> <li><li><span class="keyword">case <span class="string">' Set ':</span> </span></li></li> <li class="alt"><li class="alt">self::<span class="vars">$memberdata [<span class="vars">$feature] [<span class="vars">$name] = <span class="vars">$arguments [0]; </span></span></span></span></li></li> <li><li>break <span class="keyword">; </span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="keyword">case <span class="string">' Get ':</span> </span></li></li> <li class="alt"><li class="alt"><span class="keyword">return isset (self::<span class="vars">$memberdata [<span class="vars">$feature] [<span class="vars">$name])? self::<span class="vars">$memberdata [<span class="vars">$feature<span class="vars">] [$name]  : <span class="string">"; </span></span></span></span></span></span></span></span></li></li> <li><li>break <span class="keyword">; </span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="keyword">default:</span></li></li> <li class="alt"><li class="alt">}</li></li> <li><li></li></li> <li class="alt"><li class="alt">}</li></li> <li><li></li></li> <li class="alt"><li class="alt">}</li></li> <li><li></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="keyword">Class User <span class="keyword">extends member{</span></span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="keyword">public <span class="keyword">static <span class="keyword">function show () {</span> </span> </span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="vars">$feature = Get_called_class (); </span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="keyword">if (self::<span class="vars">$memberdata [<span class="vars">$feature]) {</span> </span> </span></li></li> <li class="alt"><li class="alt"><span class="keyword">foreach (self::<span class="vars">$memberdata [<span class="vars">$feature] <span class="keyword">as <span class="vars">$key = =<span class="vars">$member) {</span> </span> </span> </span> </span> </span></li></li> <li><li><span class="func">echo <span class="vars">$key.<span class="string">': '. <span class="vars"> $member.  <span class="string">' <br> '; </span></span></span></span></span></li></li> <li class="alt"><li class="alt">}</li></li> <li><li>}</li></li> <li class="alt"><li class="alt">}</li></li> <li><li></li></li> <li class="alt"><li class="alt">}</li></li> <li><li></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="keyword">Class profession <span class="keyword">extends member{</span></span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="keyword">public <span class="keyword">static <span class="keyword">function show () {</span> </span> </span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="vars">$feature = Get_called_class (); </span></li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="keyword">if (self::<span class="vars">$memberdata [<span class="vars">$feature]) {</span> </span> </span></li></li> <li class="alt"><li class="alt"><span class="keyword">foreach (self::<span class="vars">$memberdata [<span class="vars">$feature] <span class="keyword">as <span class="vars">$key = =<span class="vars">$member) {</span> </span> </span> </span> </span> </span></li></li> <li><li><span class="func">echo <span class="vars">$key.<span class="string">': '. <span class="vars"> $member.  <span class="string">' <br> '; </span></span></span></span></span></li></li> <li class="alt"><li class="alt">}</li></li> <li><li>}</li></li> <li class="alt"><li class="alt">}</li></li> <li><li></li></li> <li class="alt"><li class="alt">}</li></li> <li><li></li></li> <li class="alt"><li class="alt">User::set_name (<span class="string">' Fdipzone '); </span></li></li> <li><li>User::set_age (29);</li></li> <li class="alt"><li class="alt">User::show ();</li></li> <li><li></li></li> <li class="alt"><li class="alt">Profession::set_profession (<span class="string">' IT SERVICE '); </span></li></li> <li><li>Profession::set_price (2500);</li></li> <li class="alt"><li class="alt">Profession::show ();</li></li> <li><li></li></li> <li class="alt"><li class="alt">?></li></li> </ol> </ol><p><p>PHP __call and __callstatic</p></p></span>

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.