Step-by-Step Learning PHP (6) object-oriented _php Foundation

Source: Internet
Author: User
But we know that Object-oriented has three main features: inheritance, polymorphism, and encapsulation.

1. Inheritance

We continue with the example in the previous section, in PHP, inheritance and Java are the same, using the extends keyword.
Copy Code code 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 the main and we have access to the parent class in C # with base, in Java with Super, but in PHP we use the parent keyword.

If we want to access our own methods, we can use this or self.
Copy Code code 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 Code code 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

Next is the interface:
Copy Code code 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 ();
?>

There's nothing to talk about, exactly like Java.

4. Construction Method

Always forget to say that the construction method, in fact, is a piece of the same code:
Copy Code code 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);
}
}
$p =new person ("Kym", 22);
$p->sayhello ();
?>

We may often encounter a kind of abnormal type in the interview, that is, the relationship between several classes, and then the constructor and what the transfer to. However, this is not the case in PHP, because the constructor chain is not supported in PHP, which means that when you initialize a subclass, he does not automatically invoke the constructor method of the parent class.
Copy Code code 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

Destructors are different from C # and C + +, in PHP, the name of the destructor is __destructor ().
Copy Code code 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'm going to be unloaded");
}
}

6. polymorphism

Because of the existence of default parameters, as well as the weak type of PHP, the compile-time polymorphism (i.e. polymorphism due to the number of parameters and different types) is not possible, but Run-time polymorphism is mentioned above. Don't repeat it.

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.