Inheritance of object-oriented programming in PHP

Source: Internet
Author: User

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.

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.