Multiple functions use the same name, but the parameter table, that is, the number of parameters or (and) data types can be different, when invoked, 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 object support is much more powerful.
For polymorphic implementations, PHP4 only supports overrides (override), not overloads (overload). But we can use some tricks to "simulate" the implementation of the overload.
While PHP5 can support overrides and overloads, overloading is a significant difference in the implementation of other languages.
1, "simulate" overload in PHP4
Try the following code:
<?php
Choose to perform different methods based on the number of parameters (simulate "overload" in PHP4) (one of polymorphism)
Class Myclass
{
function Myclass ()
{
$method = "Method". Func_num_args ();
$this-> $method ();
}
function Method1 ($x)
{
echo "Method1";
}
function Method2 ($x, $y)
{
Echo ' METHOD2 ';
}
}
Using this class is transparent to the user through additional processing in the class:
$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 parameters. The above examples can be improved by combining functions Func_get_arg (i) and Func_get_args ().
2, using overloads in PHP5
Let's look at the following examples:
Copy Code code as follows:
<?php
Class Myclass
{
Public $attriable;
Public $one = "it is one";
Public $two = ' This is two ';
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 of this example, in PHP is not correct!
?>
People who have used C + +, Java, and C # overloads are accustomed to writing the above overloaded PHP code. But this is not true in the PHP5. PHP5 is not a parody of the aforementioned languages, but has its own set of methods for implementing overloaded methods (good or bad, not discussed here). Although the PHP5 class is a lot more powerful than PHP4, the problem of overloading is not as "improved" as we expected. In a strong type of language, you can implement overloads by using different parameter types, such as C + +, Java, C #, and so on. In the language passed by fixed parameters, you can also pass the number of parameters, such as Java, but PHP is a weakly typed language, so there is no such "overload."
PHP5 overload can be done by __get, __set, and __call several special methods. PHP calls these methods when the Zend engine attempts to access a member and is not found.
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 disallow setting property values, starting with a certain prefix or containing a certain type of value. The __call method shows how you can invoke an undefined method. When you call an undefined method, the method name and the parameters received by the method are passed to the __call method, and PHP passes the __call value back to the undefined method.
Copy Code code as follows:
<?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 ("What you pass in is an object that reads as follows:<br>");
Print_r ($p);
echo "}
Public Function Displayarray ($p)
{
Echo ("You are passing in the array, the contents are as follows:<br>");
Print_r ($p);
echo "}
Public Function Displayscalar ($p)
{
Echo ("You are passing in a separate variable that reads as follows:<br>". $p);
echo "}
}
$o = new Overloader ();
Call __set () to assign a value to a nonexistent property variable
$o->dynaprop = "Dynamic Content";
Call __get ()
Print ($o->dynaprop. "<br>\n");
Call __call ()
$o->dynamethod ("Leon", "Zeev");
$o->display (Array (1,2,3));
$o->display (' Cat ');
?>
In the above code, the display () method is invoked, and the corresponding code snippet in the class can be invoked based on the type and number of parameters, thus implementing the overload of the object method.