PHP Object-oriented method overloading two versions comparison _php tutorial

Source: Internet
Author: User
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:

Perform different methods depending on the number of parameters (simulate "overloads" in PHP4 (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:

Copy CodeThe code is as follows:
Class Myclass
{
Public $attriable;
Public $one = "it is one";
Public $two = "This is the";

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!
?>


Using C + +, Java, C # overloaded people, it is customary to write the above overloaded PHP code implementation. 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.

Overloading in PHP5 can be done through __get, __set, and __call several special methods. 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 shows you how to invoke an undefined method. When you call an undefined method, the method name and the parameter received by the method are passed to the __call method, and the value of the PHP pass __call is returned to the undefined method.

Copy CodeThe code is as follows:
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 ()
\ 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 with the following content:
");
Print_r ($p);
echo "";
}

Public Function Displayarray ($p)
{
Echo ("You passed in the array, the contents are as follows:
");
Print_r ($p);
echo "";
}

Public Function Displayscalar ($p)
{
Echo ("You're passing in a separate variable with the following:
" . $p);
echo "";
}
}

$o = new Overloader ();

Call __set () to assign a value to a non-existent property variable
$o->dynaprop = "Dynamic Content";

Call __get ()
Print ($o->dynaprop. "
\ n ");

Call __call ()
$o->dynamethod ("Leon", "Zeev");

$o->display (array);
$o->display (' Cat ');
?>


In the above code, 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.

http://www.bkjia.com/PHPjc/319404.html www.bkjia.com true http://www.bkjia.com/PHPjc/319404.html techarticle 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 be automatically called to ...

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