Detailed explanation of overloaded parent class in object-oriented neutron class of PHP

Source: Internet
Author: User
Tags constructor inheritance


Because a function with the same name cannot exist in PHP, it is not possible to define a method of duplicate names in the same class. Overloading here refers to the way in which a subclass can define a method that has the same name as the parent and overrides a method inherited from the parent class.


Methods for overloading parent classes in subclasses

The code is as follows Copy Code


<?php
Class person{

Public $name;

Public function __construct ($name = "") {
$this->name = $name;

}
Public function say () {
echo "My name". $this->name;
}

}
?>
<?php
Class Student extends person{

Public $name;

Public function __construct ($name = "") {
$this->name = $name;

}
This defines a method that has the same name as the parent class, overrides the speech method in the parent class, and overrides the
Public function say () {
echo "My Name" $this->name. ", 25 years old this year";
}

}
?>

Overriding methods and access permissions

Overridden methods in subclasses cannot use more restrictive access than overridden methods in the parent class.

If the method in the parent class has access to the protected, the permission of the method overridden in the subclass is protected or public, and if the method in the parent class is public, the permission for the method that the subclass is overriding is public only. Maybe that's why a subclass can inherit the private members of a parent class, but it can't be used.


Number of arguments When overridden

Subclasses can have a different number of parameters than the parent class, as in the following construction method, a parameter $age is added.

The code is as follows Copy Code

<?php

Class Student extends person{

Public $name;
Public $age;

Public function __construct ($name = "", $age =25) {
$this->name = $name;

$this->age = $age;

}

Public function say () {

echo "My Name" $this->name. ", this year". $this->age. " Years old ";

}

}
?>

constructor override

The aforementioned "Number of arguments when overridden" has implemented a subclass that overrides the constructor of the parent class. But this is not a good way to write, if you look closely, you will find that the upper subclass student the parent person constructor, in fact, in the construction of the parent class function to add more than one parameter to the but also the original parameters of the parent class to write again, because the parent class person's constructor has only one parameter, so we write again do not feel any trouble, but if the argument more than one, but a few or more, then you will find its cumbersome, So is there a way to simplify the problem? The answer is yes, you can invoke the method overridden in the parent class in the overloaded method of the subclass by using "Parent:: Method name." If you use "parent::__construct ()" To invoke the overridden construction method in the parent class, the other methods are similar, so the above code can be simplified to:

The code is as follows Copy Code

<?php

Class Student extends person{

Public $name;

Public $age;

Public function __construct ($name = "", $age =25) {

Parent::__construct ($name, $age);

$this->age = $age;
}

Public function say () {
Parent::say ();

echo ", this year". $this->age. " Years old ";

}

}
?>

Look at one more example

PHP5 Rewrite method
Set a parent class first, which is the "Dog" class, which describes the characteristics of the Dog.

Dog has 2 eyes, can run, will bark. Let's just describe it.

I keep a dog, it's a puppy, it conforms to the characteristics of the dog class, but it is different.

My puppy has a name, my puppy is too small, won't shout loudly, will only hum.

We use the concept of inheritance to achieve this design.

The code is as follows Copy Code

?
Dogs have two eyes, they bark and they run.
Class Dog {
protected $eyeNumber = 2; Property
Returns the method that encapsulates the property.
Public Function Geteyenumber () {
return $this->eyenumber;
}
Dogs will bark.
Public Function Yaff () {
Return "Dog Yaff, Wang. Wang ... ";
}
Dogs can run.
Public Function run () {
Return "Dog run. Running ... ";
}
}
$dog = new Dog ();
echo "Dog have". $dog->geteyenumber (). "Eyes. <br> ";
echo $dog->yaff (). <br> ". $dog->run ();
echo "<br><br>";
This is my puppy called "the Dog", it is very small. will not bark, only hum hum.
Class Mydog extends Dog {
Private $name = "dog";
Public Function GetName () {
return $this->name;
}
Public Function Yaff () {
return $this->name. "Yaff, Heng...heng ...";
}
}
$myDog = new Mydog ();
echo $myDog->getname (). "Have". $myDog->geteyenumber (). "Eyes. <br> ";
echo $myDog->yaff (). <br> ". $myDog->run ();
?>

Program Run Result:

Dog have 2 eyes.
Dog Yaff, Wang. Wang..
Dog run. Running ...

The dog have 2 eyes.
Dog Yaff, Heng...heng.
Dog run. Running ...

Overriding methods and access permissions
Overridden methods in subclasses cannot use more restrictive access than overridden methods in the parent class.

The parent class is private when the public child class is public.

The code is as follows Copy Code

?
Simplifies the dog class and the Mydog class to demonstrate overridden access rights.
Class Dog {
protected $eyeNumber = 2; Property
Returns the method that encapsulates the property.
Public Function Geteyenumber () {
return $this->eyenumber;
}
}

Class Mydog extends Dog {
protected function Geteyenumber () {
return $this->eyenumber;
}
}
/*
Class Mydog extends Dog {
Private Function Geteyenumber () {
return $this->eyenumber;
}
}
*/

?>

Program Run Result:
Fatal error:access level to Mydog::geteyenumber () must is public (as in class Dog) in e:phpprojectstest.php on line 15

The parent class is protected when the public child class is a.

  code is as follows copy code

Simplifies the dog class and the Mydog class to demonstrate overridden access rights.
Class Dog {
 protected  $eyeNumber = 2;//property
 //Returns the method that encapsulates the property.
 public function Geteyenum ber () {
  return $this->eyenumber
 } 
}

Class Mydog extends Dog {
&NBSP;PR ivate function Geteyenumber () {
  return $this->eyenumber;
 } 
}

Program Run Result:

 fatal error:access level to Mydog::geteyenumber () must is public (as in class Dog) in e:phpprojectstest.php on L ine

Number of arguments When overridden
Subclasses can have a different number of parameters than the parent class. (This differs from Java in that PHP is a weakly typed language.) )

The code is as follows Copy Code


?
Simplifies the dog class and the Mydog class, demonstrating the parameters of the overridden method.
Class Dog {
protected $eyeNumber = 2; Property
Returns the method that encapsulates the property.
Public Function Geteyenumber () {
return $this->eyenumber;
}
}
Class Mydog extends Dog {
The overridden method has a different number of parameters than the method of the parent class.
Public Function Geteyenumber ($eys) {
$this->eyenumber = $eys;
return $this->eyenumber;
}
}

$myDog = new Mydog ();
echo "My dog Hava". $myDog->geteyenumber (3). "Eyes.";
Sao Tian Dog. Ha..
The following sentence will report a missing parameter error.
echo "My dog Hava". $myDog->geteyenumber (). "Eyes.";
?>

Program Run Result:

My dog Hava 3 eyes.

constructor override

In the following example, both the parent class and the subclass have their own constructors, and when the subclass is instantiated, the constructor of the subclass is invoked, and the constructor of the parent class is not invoked, please compare the constructor inheritance of the first section.

  code is as follows copy code

.
//2-2/extends1.php
//constructor inheritance issues.
Class animal{
  Public $legNum = 0;
 public function __construct () {
   $this->legnum = 4;
  echo "I am an Animal<b R> ";
 }
}
Class Dog1 extends Animal {
 public function __construct () {
   $this->legnum = 4;
  echo "I am a Dog .<br>";
 }
}
$dog 1 = new Dog1 ();
Echo <br>;
echo  "Legnum is". $dog 1->legnum;
/*
instantiating subclasses. The constructor is called.
*/

Program run Result:

 i am a Dog . 
Legnum is 4

Note: This differs from Java in that constructors in Java cannot be inherited, and when subclasses are instantiated, the constructors of subclasses are invoked and the constructors of the parent class are invoked.

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.