In the previous articleArticle, We mainly talk about how to create a class and object and some basic usage.
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.
class People
{< br> private $ name ;
Public Function getname ()
{< br> return $ this -> name;
}< br> Public Function setname ( $ name )
{< br> $ this -> name = $ name ;
}< BR >}< br> class Student extends People
{< br> private $ Grade ;
Public Function sayhello ()
{< br> echo ( "Good morning, ". parent : getname ();
}< BR >}
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.
ClassStudentExtendsPeople
{
Public FunctionGetname ()
{
Return"Kym";
}
Private$ Grade;
Public FunctionSayhello ()
{
Echo("Good morning ,".Self: Getname ());
// Echo ("good morning,". $ this-> getname ());
}
}
2. abstract class
When it comes to inheritance, You Have To Say abstract classes.
<? 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:
<? 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
I forgot to talk about the constructor, which is actually the sameCode:
<? 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 );
}
}
$ 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.
<? 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 ().
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.