Php object-oriented method reloading comparison between two versions

Source: Internet
Author: User

Multiple Functions use the same name, but the parameter table, that is, the number of parameters or (and) data types can be different. Although the method name is the same during the call, however, the corresponding function can be automatically called according to the parameter table.

PHP4 only implements some simple object-oriented functions, and PHP5 will be much more powerful in support of objects in the future.

For the implementation of polymorphism, PHP4 only supports override, but does not support overload ). However, we can use some techniques to simulate the implementation of heavy loads.

Although PHP5 supports overwrite and reload, the specific implementation of reload is quite different from other languages.

1. "simulate" overloading in PHP4

Compile the following code:

<? Php
// Execute different methods based on the number of parameters (simulate "overload" in PHP4 (a type of polymorphism)

Class Myclass
{
Function Myclass ()
{
$ Method = "method". func_num_args ();
$ This-> $ method ();
}

Function method1 ($ x)
{
Echo "method1 ";
}

Function method2 ($ x, $ y)
{
Echo 'method2 ';
}
}

// This class is transparent to users through additional processing in the class:
$ Obj1 = new Myclass ('A'); // call method1
$ Obj2 = new Myclass ('B', 'C'); // call method2
?>

In the above Code, the number of parameters is obtained by using the func_num_args () function in the constructor, And the method1 or method2 method is automatically executed. We can improve the above example by combining the functions func_get_arg (I) and func_get_args.

2. Use overload in PHP5

First look at the following example:

Copy codeThe Code is as follows:
<? Php
Class Myclass
{
Public $ attriable;
Public $ one = "this 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 ();

// This example is incorrect in PHP!
?>


People who have used C ++, Java, and C # reloads will write the above PHP code for implementing the reload. But this is incorrect in PHP5. PHP5 does not imitate the above languages, but has its own set of methods to overload implementation (good or bad, which is not discussed here ). Although the PHP5 class is much more powerful than PHP4, It is not "Improved" on the "overload" issue as we expected ". In "strong" languages, you can use different parameter types to implement "overloading", such as C ++, Java, and C. In the language passed by "fixed parameters", the number of parameters can also be passed, such as Java, but PHP is a weak language, so there is no similar "overload ".

In PHP5, you can use several special methods such as _ get, _ set, and _ call. PHP will call these methods when the Zend engine tries to access a member that is not found.

In the following example, __get and _ set replace all accesses to the attribute variable array. If necessary, you can implement any type of filter you want. For example, a script can disable attribute values and use a certain prefix or contain certain types of values at the beginning. The _ call method describes how to call an undefined method. When you call an undefined method, the method name and parameters received by the method will be passed to the _ call method, and the value of _ call passed by PHP will be returned to the undefined method.

Copy codeThe Code is 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 ("You passed in an object with the following content: <br> ");
Print_r ($ p );
Echo "}

Public function displayArray ($ p)
{
Echo ("You input an array with the following content: <br> ");
Print_r ($ p );
Echo "}

Public function displayScalar ($ p)
{
Echo ("You passed in a separate variable, the content is as follows: <br>". $ p );
Echo "}
}

$ O = new Overloader ();

// Call _ set () to assign a value to a non-existent attribute 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 called. You can call the corresponding code segment in the class based on the type and number of parameters, so as to overload 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.