Why do I need to inherit?
<?php
Develop a set of student management system (pupil pupil, college students, ...)
Parent class
Class stu{
Public $name;
protected $age;
protected $grade;
Public Function Showinfo () {
echo $this->name. "| |" $this->age;
}
}
Sub Class
Class Pupil extends stu{//pupil
Public Function testing () {
echo "Pupil examination";
}
}
Class Graduate extends stu{//graduate student
Public Function testing () {
echo "Postgraduate examination";
}
}
Create a student
$stu 1=new pupil ();
$stu 1->testing ();
$stu 1->name= "www.bianceng.cn";
$stu 1->showinfo ();
echo "<br/>";
Create a graduate student
$stu 2=new graduate ();
$stu 2->testing ();
$stu 2->name= "Hello";
$stu 2->showinfo ();
?>
From the above code can be seen, the so-called inheritance, is a subclass by extends the parent class, the parent class Public/protect properties and methods inherited, but can not inherit the parent class private properties and methods.
Basic syntax for inheritance:
Class Name extends parent class name {
Attributes and methods required by subclasses
}
Considerations for using Inheritance:
1 subclasses can inherit the Public/protect properties and methods of the parent class, but cannot inherit the private properties and methods of the parent class.
<?php
What properties and methods of the parent class inherit from the Quilt class
Class a{
Public $n 1=1;
Protected $n 2= "www.bianceng.cn";
Private $n 3=20;
Public Function test1 () {
echo "test1 ()";
}
Public Function test2 () {
echo "Test2 ()";
}
Public Function test3 () {
echo "Test3 ()";
}
}
Class Subclass extends A{
Function Show () {
echo "<br/>". $this->n2; //
}
}
Creating Subclass Objects
$sub 1=new Subclass ();
$sub 1->test1 ();
echo "<br/>";
$sub 1->test2 ();
echo "<br/>";
$sub 1->test3 ();
echo "<br/>". $sub 1->n1;
$sub 1->show ();
?>
2 PHP inheritance is a single inheritance, that is, a subclass cannot directly inherit two parent classes
You can inherit multiple classes by inheriting the two-time method.
<?php
Class a{
}
Class B extends a{
}
Class C extends b{//through two inheritance, C inherits A and B two classes
}
?>
3 When the subclass object is created, the parent class's construction method is not automatically invoked by default.
<?php
Class a{
Construction method
Public Function __construct () {
echo "A__construct";
}
}
Class B extends a{
function __construct () {
echo "B__construct";
}
}
Creating an Object instance
$b =new B (); Output b__construct
?>
4 If you want to invoke the constructor or other method (public/protected) of the parent class in the subclass, you can do so (understand):
(1) Class Name:: Method Name ();
(2) Parent:: Method name ();
Cases:
<?php
Class a{
Construction method
Public $n 1=90;
Public Function __construct ($n 1) {
$this->n1= $n 1;
echo "A__construct";
}
}
Class B extends a{
function __construct () {
echo "B__construct";
Explicit method of calling the parent class
A::__construct (900);
Parent::__construct (900);
}
}
Creating an Object instance
$b =new B (); Output b__construct
echo "<br/>". $b->n1; Output 900
?>
5 if the method in the subclass (derived class) is the same as the method in the parent class (the base class), it is called a method override/method overlay.