PHP implements class function overloading by magic method _call

Source: Internet
Author: User
PHP implements class function overloading by magic method __call

Since PHP is a weakly typed language, it is not overloaded like C + + by changing the type of function return value and the number of arguments to the function! But in the actual development may have the overloaded function the demand, in order to satisfy the development demand, we can use the Magic Method __call () to implement the function overloading !

function __call ($fun, $argv) {    if ($fun = = "Assign") {    if (count ($argv) ==1) {    $ THIS->ASSIGN1 ($argv [0]);     }    ElseIf (count ($argv) ==2) {    $this->assign2 ($argv [0], $argv [1])}    }    // The Assign function accepts  the parameter function assign2 ($key, $value) {  if (isset ($key) &&!empty ($value)) {  $this val["$key"]= $value;  } else{  exit ("ERROR: Please set Variable");  }  }  Overloaded Assign function accepts array function  ASSIGN1 ($array) {  if (!empty ($array)) {  foreach ($array as $key = $value) {  $this->val["$key"]= $value;  }  else{  exit ("ERROR: Please set array");}}}  

  • 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.