Step by step learning PHP (6) object-oriented _ PHP Tutorial

Source: Internet
Author: User
Learn PHP (6) object-oriented step by step. But we know that object-oriented has three main features: inheritance, polymorphism, and encapsulation. 1. inheritance we continue the example in the previous section. in PHP, inheritance is the same as Java and uses extends. 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.

The 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.

The 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.

The code is as follows:


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:

The code is as follows:


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 have forgotten the constructor method, which is actually the same code:

The code is as follows:


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.

The code is as follows:


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 ().

The 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.

Bytes. 1. inherit from the example in the previous section. in PHP, inheritance is the same as in Java, and extends is used...

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.