Php Method overloading

Source: Internet
Author: User
Tags php language

<?php
/*
* PHP Object-oriented overrides and overloads
Rewrite:
That is, when a subclass inherits some of the methods of the parent class, and the subclass defines the same method within it, the newly defined method overrides the inherited method of the parent class, and the subclass can only invoke its internally defined methods.

There are several requirements:

1. When a parent class and subclass have a method that has exactly the same parameters and names, the subclass method overrides the method of the parent class.

The method overrides are implemented only if the parameters are consistent. When the number of arguments is inconsistent, an error is reported (this involves the method overload described above). When the method name is inconsistent, it is not overwritten, just the new method defined by the subclass.

2. The access modifier can be different when the method is overridden, but the access scope of the subclass must be greater than or equal to the access scope of the parent class.

This is the PHP language design when the rules. I understand that it is easier to access the high level of things, if you go to the bottom of the things to access the permissions must be higher.

Requires the same parameter as the name. The subclass is not required and the parent class name is the same.

is to require the same parameter as the name, which requires that the number of parameters is the same as the parent class, not the parameter name. That is, the passed parameter name can be any, as long as the number of guaranteed delivery is the same.

Look at the code:
*/

Class Shao
{
Public function Sasa ($name) {
echo $name, ' Dog! <br/> ';
}
}
Class Zhen extends shao{
Public Function aaa ()
{
Echo ' Cat <br/> ';
}
}
$lli = new Zhen ();
$lli->aaa ();
$lli->aaa (' binghh ');
?>


<?php
Class Fu
{
Public Function text () {
Echo ' parent class ';
}
Public Function Textt () {
Echo ' father ';
}
}
Class Erzi extends fu{
Public Function text ()
{
Parent::text ();//I want something of the parent class
Echo ' son ';//rewrite

}
}
$shili = new Fu ();
$erzi = new Erzi ();
$erzi->text ();
?>
<?php
/**
Overload:
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. That is, when called, although the method has the same name, the corresponding function can be called automatically according to the different parameters.
PHP is not easy to implement.
You can make a method inside a class produce multiple effects, and different logic can be executed depending on the parameters passed in.
There are several requirements:

1, in the use of overloading can only be achieved by the same method name, different parameter form. Different parameter types can be different parameter types, different parameter numbers, different parameter order (parameter types must not be the same);
2, can not be overloaded by access rights, return type, thrown exception;
3, the method of the exception type and number will not affect the overload;
A little complicated code is not allowed ...
*/
Class Fu
{
Public Function text () {
Echo ' parent class ';
}
Public Function Textt () {
Echo ' father ';
}
}
Class Erzi extends Fu
{
function test ($name)
{
Method overloading
Parent::text ();//I want something of the parent class
echo "Erzi", $name;//rewrite

}
}

$erzi = new Fu ();
$erzi-Text ("123");

Copy Code
Overrides exist only in subclasses and parent classes, and overloads exist in a class.
//
Override is a rewrite
//
Overload is overloaded
?>

Php Method overloading

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.