Step by step learn PHP of 6 object oriented

  • 2020-03-31 20:19:13
  • OfStack

But as we know, object-oriented has three characteristics: inheritance, polymorphism, and encapsulation.

1. The inheritance

Continuing with the example in the previous section, inheritance in PHP is the same as in Java, using the extends keyword.
 
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()); 
} 
} 

In this case, the main thing we need is for us to access the parent class we use 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 we can use self.
 
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. An abstract class

When we talk about inheritance, we have to talk about 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. The interface

Here's 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 to run "); 
} 
} 
$s=new Student(); 
$s->SetName("kym"); 
$s->SayHello(); 
$s->Run(); 
?> 

Nothing to say, just like Java.

4. Construction method

I forgot to mention the constructor, but it's just the same piece of code:
 
<?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 perverted type of question in an interview, which is the relationship between several classes, and then the constructor and so on. This is not the case in PHP, however, because the constructor chain is not supported in PHP, which means that when you initialize a child class, it does not automatically call the parent constructor.
 
<?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

Destructor unlike C# and C++, in PHP the destructor name is s/s 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'm going to be unloaded "); 
} 
} 

6. Polymorphism

Because of the default parameters and the weak typing of PHP, compile-time polymorphism (that is, polymorphism due to the number and type of parameters) is not possible, but runtime polymorphism was mentioned above. I won't go into that.

Related articles: