Examples Compare Php method overloads in two ways

Source: Internet
Author: User
Tags call back
Overloading is an implementation of the polymorphism of a class. function overloading means that an identifier is used as multiple function names, and the function can be distinguished by the number of arguments or parameter types of the function, and the call does not get confused. The main benefit of this is that you do not have to write multiple functions in order to have different parameter types or number of arguments.

Multiple functions with the same name, but the parameter table, that is, the number of parameters or (and) data type can be different, when called, although the method name is the same, but according to the parameter table can automatically call the corresponding function.

PHP4 only implements the object-oriented part of the simple function, and PHP5 later on the object support is much more powerful.

For polymorphic implementations, the PHP4 only supports overwrite (override) and does not support overloading (overload). But there are some tricks we can use to "emulate" the implementation of overloading.

Although the PHP5 can support overrides and overloads, overloading is more significant than other languages, depending on the implementation.

1, "Analog" overload in PHP4

Sample the following code:

<?php//Choose to perform different methods depending on the number of parameters (in PHP4, simulate "overloaded" (a polymorphic One) class Myclass {function Myclass () {$method = "method". Func_num_args () ; $this $method (); } function Method1 ($x) {echo ' method1 ';} function Method2 ($x, $y) {echo ' Method2 ';}} By additional processing in the class, using this class is transparent to the user: $obj 1 = new Myclass (' A '); Will call method1 $obj 2 = new Myclass (' B ', ' C '); Will call Method2?>

In the above code, the Method1 or Method2 method is automatically executed by using the Func_num_args () function in the constructor to take the number of arguments. We can combine the functions func_get_arg (i) and Func_get_args () to improve the above example.

2, using overloads in PHP5

Let's look at the following example:
The code is as follows:

<?php class Myclass {public $attriable, public $one = ' This is one ', public $two = ' This is ', function construct () {} function one ($one) {$this->one= $one; $this->attriable = $this->one;} function One ($one, $two) {$this One= $one; $this->two= $two; $this->attriable = $this->one. $this->two; } function display () {echo $this->attriable;}} $one = "This Is my class"; $two = "Im the best"; $myclass = new MyClass (); $myclass->one ($one); $myclass->display (); $myclass->one ($one, $two); $myclass->display (); The practice in this example is not correct in PHP!?>


People who have used the C + +, Java, C # overloads, are accustomed to writing the PHP code for the overloaded implementations above. But this is not true in the PHP5. PHP5 is not a parody of the aforementioned languages, but rather has its own set of methods for implementing overloaded methods (good or bad, not discussed here). Although the PHP5 class is much more powerful than PHP4, the problem of "overloading" does not "improve" as we expected. In a "strong" language, "overloads" can be implemented by different parameter types, such as C + +, Java, C #, and so on. In the language passed by "fixed arguments", it can also be passed by the number of parameters, such as Java, but PHP is a weakly typed language, so there will be no "overloads" like these. The overloads in the

PHP5 can be done through a few special methods, get, set, and call. PHP will call these methods when the Zend engine tries to access a member and does not find it.

In the following example, get and set replace all access to an array of property variables. If necessary, you can also implement any type of filter you want. For example, a script can prohibit setting a property value, starting with a certain prefix or containing a certain type of value. The call method illustrates how you can invoke an undefined method. When you call an undefined method, the method name and the parameter received by the method are passed to the calling method, and PHP passes the value of call back to the undefined method.

<?php class Overloader {Private $properties = array (), function get ($property _name) {if (Isset ($this->properties[$ Property_name]) {return ($this->properties[$property _name]);} else {return (NULL);}}  function set ($property _name, $value) {$this->properties[$property _name] = $value;} public Function call ($method, $p) {print ("invoking $method () <br>\n");//print ("Arguments:"); Print_r ($args); if ($method = = ' Display ') {if (Is_object ($p [0])) $this->displayobject ($p [0]), else if (Is_array ($p [0])) $this Displayarray ($p [0]); else $this->displayscalar ($p [0]); }} Public Function Displayobject ($p) {echo ("You passed in an object, the contents are:<br>"); Print_r ($p); echo "

In the code above, the display () method is called, and the corresponding code snippet in the class can be called based on the type and number of parameters, thereby overloading the object method.

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.