PHP Object-oriented-detailed simulation of method overloading in the general object-oriented language (overload)

Source: Internet
Author: User
A method overload in a generic object-oriented design language (such as C++,java) is to define the same method name, and to access our different methods of the same method name, depending on the number of arguments or the type of the parameter. However, in PHP, the method is not overloaded, because PHP is a weak type of language, so in the parameters of the method itself can receive different types of data, and because the PHP method can receive an indefinite number of parameters, so by passing different number of parameters to call the different methods of the method is not valid. So there is no traditional overloaded (overload) method in PHP, but due to PHP's flexibility, it is possible to emulate overloaded methods in general object-oriented languages.

Simulating method overloads in general object-oriented languages in PHP (overload)

Let's start with an example:

<?php  /* rewrite/overwrite override  means: The subclass overrides the method overload of the parent class with the same name:  overload  refers to: There are multiple methods with the same name, but different parameter types/numbers. Different arguments are passed, and different methods are called.  However, in PHP, multiple methods with the same name are not allowed. Therefore, it is not possible to complete this overload in the java,c++.  However, PHP is flexible enough to achieve a similar effect *   ///////////////////To simulate overloading in PHP  class Calc {public       function area () {   ///        /// Determine the number of parameters that are obtained when an area is called           $args = Func_get_args ();           if (count ($args) = = 1) {   //Only one parameter            return 3.14 * $args [0] * $args [0]   ; Calculate the area of the circle         } else if (count ($args) = = 2) {   //have 2 parameters            return $args [0] * $args [1];   Find the area of the rectangle        } else {               return ' unknown graph ';           }}   }   $calc = new Calc ();   Calculate the area of the Circle  Echo $calc->area (Ten), ' <br/> ';   Calculates the area of the rectangle   echo $calc->area (5,8);  ? >

Using PHP's overloaded (overloading) technology to implement method overloading in general object-oriented languages (overload)

The following example uses the overloaded (overloading) technique of PHP to implement the traditional method overloading (overload) using the Magic method.

For overloaded technologies in PHP, refer to: PHP Object-oriented overloading (overloading)

Example:

<?php/* Target: Design A class, this class instance, can realize the following requirements: Call method F1: Pass in 1 parameters, return its own, passed 2 parameters, the sum of squares, passed 3 parameters, the cubic and other parameters, the form, will be error!            */class a{//This is a magic method that when an object of a does not invoke a method that does not exist//is automatically called to respond to this situation function call ($Func _name, $argument) {//Use F1 does not exist if ($Func _name = = = ' F1 ') {$len = count ($argument) and//number of arguments obtained if ($len <1 | | $len ; 3) {Trigger_error ("The number of parameters is incorrect!             ", E_user_error);               }else if ($len = = 1) {return $argument [0];             }else if ($len = = 2) {return $argument [0]* $argument [0] + $argument [1]* $argument [1];                            }else if ($len = = 3) {$v 1 = $argument [0];                            $v 2 = $argument [1];                            $v 3 = $argument [2];            return $v 1* $v 1* $v 1 + POW ($v 2, 3) + POW ($v 3, 3);    }}else if ($Func _name = = = ' F2 ') {//Another non-existent method}else if ($Func _name = = = ' F3 ') {//...} }} $a = new A (); $v 1 = $a-&GT;F1 (1); $v 2 = $a->f1 (2,3); $v 3 = $a->f1 (4,5,6); echo "v1= $v 1, v2 = $v 2, v3 = $v 3";? >

The result of the operation is:

v1= 1, v2 = V3 = 405
Related Article

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.