The polymorphism of object-oriented programming in PHP

Source: Internet
Author: User

Php method overload (Overload)

Overloading is an implementation of the polymorphism of a class. A function overload is an identifier that is used as a function name and can be distinguished by the number of parameters of the function or by the type of the parameter, which is not confused when invoked. Call, although the method name is the same, but according to the parameter table can automatically call the corresponding function.

PHP5 supports overloading, but it is quite different from other programming languages in the concrete implementation of overloading.

<?php
Class a{
Public Function test1 () {
echo "Test1";
}
Public function test1 ($a) {
echo "Hello";
}
}
$a =new A ();
$a->test1 ();
?>

This usage is not correct, the runtime will be an error: Fatal error:cannot redeclare a::test1 () in ...

PHP5 How to implement overloading (officially not recommended)

PHP5 uses the Magic function __call to implement the function overload.

PHP5 the direct method overload is not supported by default, you can use __call to simulate the effect of the method overload.

<?php
Class a{
Public function Test1 ($p) {
echo "receives an argument";
}
Public Function Test2 ($p) {
echo "receives two parameters". __class__; __class__ is a magic constant that displays the current class name
}
Using Magic method to __call implement method overload
__call is an object that invokes a method and the method does not exist, the system automatically invokes the __call
function __call ($method, $p) {
if ($method = = "Test") {
if (count ($p) ==1) {
$this->test1 ($p);
}else if (count ($p) ==2) {
$this->test2 ($p);
}
}
}
}
$a =new A ();
$a->test (1); Output: Receiving a parameter
echo "<br/>";
$a->test (100,2);
echo "<br/>". __line__; The following is the use of the Magic constants
echo "<br/>". __file__;
echo "<br/>". __dir__;
?>

Rewrite/method overrides for PHP methods (override)

When a parent class knows that all subclasses have a method, but the parent cannot determine the content of the method, you can have subclasses override this method.

1 to implement overrides, the function names and parameters of the parent class and subclass need to be exactly the same

2 If a subclass needs to invoke a method of the parent class (public/protected), you can use the

Parent: Method Name (parameter ...);

Or

Parent class Name: Method name (Parameter ...)

Example: the Sound of Animals

<?php
Abstract a animal class
Class animal{
Public $name;
protected $price;
Function Cry () {
echo "Temporarily does not know, the parent class is not tentative";
}
}
Class Dog extends animal{
Function Cry () { //Cry method that overrides the parent class
echo "Dog barking!" ";
}
}
Class Pig extends animal{
Function Cry () { //Cry method that overrides the parent class
echo "The Pig hums!" ";
echo "<BR/". Parent::cry (); Method of calling the parent class in a subclass
}
}
Create a dog
$dog 1=new Dog ();
$dog 1->cry ();
$pig 1=new Pig ();
$pig 1->cry ();
?>

3 When the method overrides, the access modifier can be different, but the access scope of the subclass must >= the parent class's

<?php
When implementing a method overlay, the access modifiers can be different,
But must be satisfied: the scope of the child class is to >= the parent class's access scope
Class animal{
Public $name;
protected $price;
Cry method of overriding parent class
Public Function Cry () {
echo "Temporarily does not know, the parent class is not tentative";
}
}
Class Dog extends animal{
protected function Cry () {//This sentence will be an error because the parent class's cry () is public, and the subclass's protected permissions are less than the parent class's
echo "Dog barking!" ";
}
}
Class Pig extends animal{
Function Cry () {
echo "The Pig hums!" ";
Methods to invoke the parent class
echo "<BR/". Parent::cry ();
}
}
Create a dog
$dog 1=new Dog ();
$dog 1->cry ();
$pig 1=new Pig ();
$pig 1->cry ();
?>

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.