But we know that Object-oriented has three main features: inheritance, polymorphism, and encapsulation.
1. Inheritance
Let's continue with the example in the previous section. In PHP, the inheritance is the same as in Java, and the extends keyword is used.CopyCodeThe Code is as follows: Class People
{
Private $ name;
Public Function getname ()
{
Return $ this-> name;
}
Public Function setname ($ name)
{
$ This-> name = $ name;
}
}
Class student extends people
{
Private $ grade;
Public Function sayhello ()
{
Echo ("good morning,". Parent: getname ());
}
}
Here, we need to mainly access the parent class using base in C # And super in Java, but in PHP, we use the parent keyword.
If we want to access our own methods, we can use this or self.Copy codeThe Code is as follows: class student extends people
{
Public Function getname ()
{
Return "Kym ";
}
Private $ grade;
Public Function sayhello ()
{
Echo ("good morning,". SELF: getname ());
// Echo ("good morning,". $ this-> getname ());
}
}
2. abstract class
When it comes to inheritance, You Have To Say abstract classes.Copy codeThe Code is as follows: <? PHP
Abstract class people
{
Private $ name;
Public Function getname ()
{
Return $ this-> name;
}
Public Function setname ($ name)
{
$ This-> name = $ name;
}
Abstract function sayhello ();
}
Class student extends people
{
Public Function sayhello ()
{
Echo ("good morning,". Parent: getname ());
}
}
$ S = new student ();
$ S-> setname ("Kym ");
$ S-> sayhello ();
?>
3. Interface
The following is the interface:Copy codeThe Code is as follows: <? PHP
Abstract class people
{
Private $ name;
Public Function getname ()
{
Return $ this-> name;
}
Public Function setname ($ name)
{
$ This-> name = $ name;
}
Abstract function sayhello ();
}
Interface Irun
{
Function run ();
}
Class student extends people implements Irun
{
Public Function sayhello ()
{
Echo ("good morning,". Parent: getname ());
}
Public Function run ()
{
Echo ("two legs running ");
}
}
$ S = new student ();
$ S-> setname ("Kym ");
$ S-> sayhello ();
$ S-> Run ();
?>
Nothing to say, just like Java.
4. Constructor
the constructor is actually the same code: copy Code the code is as follows: class person
{< br> private $ name;
private $ age;
Public Function person ($ name, $ age)
{< br> $ this-> name = $ name;
$ this-> age = $ age;
}< br> Public Function sayhello ()
{< br> echo ("Hello, my name is ". $ this-> name. ". i'm ". $ this-> age);
}< BR >}< br> $ P = new person ("Kym", 22 );
$ p-> sayhello ();
?>
During the interview, we may often encounter an abnormal question type, that is, the relationship between several classes, and then adjust the constructor or something. However, this is not the case in PHP, because the constructor chain is not supported in PHP, that is, when you initialize the subclass, it does not automatically call the constructor of the parent class.Copy codeThe Code is as follows: <? PHP
Class person
{
Private $ name;
Private $ age;
Public Function person ($ name, $ age)
{
$ This-> name = $ name;
$ This-> age = $ age;
}
Public Function sayhello ()
{
Echo ("Hello, my name is". $ this-> name. ". I'm". $ this-> age );
}
}
Class student extends person
{
Private $ score;
Public Function student ($ name, $ age, $ score)
{
$ This-> person ($ name, $ age );
$ This-> score = $ score;
}
Public Function introduce ()
{
Parent: sayhello ();
Echo (". In this exam, I got". $ this-> score );
}
}
$ S = new student ("Kym", 22,120 );
$ S-> introduce ();
?>
5. destructor
Different from C # And C ++, in PHP, the name of the Destructor is _ destructor ().Copy codeThe Code is as follows: class student extends person
{
Private $ score;
Public Function student ($ name, $ age, $ score)
{
$ This-> person ($ name, $ age );
$ This-> score = $ score;
}
Public Function introduce ()
{
Parent: sayhello ();
Echo (". In this exam, I got". $ this-> score );
}
Function _ destruct ()
{
Echo ("I want to be uninstalled ");
}
}
6. Polymorphism
The existence of default parameters and weak PHP types make it impossible to implement polymorphism during compilation (that is, polymorphism due to the number and type of parameters, however, Runtime polymorphism is mentioned above. I will not go into details.