But we know that object-oriented has three major characteristics: inheritance, polymorphism and encapsulation.
1. Inheritance
Let’s continue the example in the previous section. In PHP, inheritance is the same as Java, both 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());
}
}
Here, the main thing we need is that we access the parent class using base in C# and super in Java, but in In PHP, we use the parent keyword.
If we want to access our own methods, we can use this or 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. Abstract class
When it comes to inheritance, we have to talk about abstract classes.
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 next step is the interface:
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("Running on two legs");
}
}
$s=new Student();
$s->SetName("kym");
$s->SayHello();
$s->Run();
?>
There is nothing to say, it is exactly the same as Java.
4. Construction method
I always forgot to mention the construction method, which is actually the same code:
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 this in interviews A perverted question type is the relationship between several classes, and then the constructors and other things are adjusted back and forth. However, you will not encounter such a situation in PHP, because PHP does not support constructor chains, that is to say, when you initialize a subclass, it will not automatically call the constructor of the parent class.
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
The destructor is 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'm going to be uninstalled");
}
}
6. Polymorphism
Due to the existence of default parameters and the weak type of PHP, compile-time polymorphism (that is, polymorphism caused by different number and types of parameters) cannot be implemented, but run-time polymorphism is Already mentioned above. No more details.

如何通过编写代码来学习PHP8中的文件操作技巧PHP是一种广泛应用于Web开发的脚本语言,能够方便地对文件进行操作,如读写文件、创建目录等。掌握PHP的文件操作技巧对于开发人员来说是非常重要的。本文将介绍如何通过编写代码来学习PHP8中的文件操作技巧。第一步:搭建PHP开发环境在学习PHP的文件操作技巧之前,我们首先需要搭建一个P

作为一名Java开发者,学习和使用Spring框架已经是一项必不可少的技能。而随着云计算和微服务的盛行,学习和使用SpringCloud成为了另一个必须要掌握的技能。SpringCloud是一个基于SpringBoot的用于快速构建分布式系统的开发工具集。它为开发者提供了一系列的组件,包括服务注册与发现、配置中心、负载均衡和断路器等,使得开发者在构建微

win7系统自带有备份还原系统的功能,如果之前有给win7系统备份的话,当电脑出现系统故障的时候,我们可以尝试通过win7还原系统修复。那么win7怎么还原系统呢?下面小编就教下大家如何还原win7系统。具体的步骤如下:1、开机在进入Windows系统启动画面之前按下F8键,然后出现系统启动菜单,选择安全模式登陆即可进入。2、进入安全模式之后,点击“开始”→“所有程序”→“附件”→“系统工具”→“系统还原”。3、最后只要选择最近手动设置过的还原点以及其他自动的还原点都可以,但是最好下一步之前点击

Java安装教程:从下载到配置一步不漏随着计算机技术的发展,Java成为了一种非常流行的编程语言。无论是在开发桌面应用程序、移动应用程序还是后端服务器,Java都广泛应用。而为了使用Java进行编码,首先需要将Java安装在计算机上。本文将详细介绍Java安装的步骤,为读者提供一步不漏的指导。一、下载Java安装程序在安装Java之前,首先需要从官方网站(h

随着Web应用程序的需求越来越高,PHP技术在开发领域中变得越来越重要。在PHP开发方面,测试是一个必要的步骤,它可以帮助开发者确保他们创建的代码在各种情况下都可靠和实用。在PHP中,一个流行的测试框架是PHPUnit。PHPUnit是一个基于Junit的测试框架,其目的是创建高质量、可维护和可重复的代码。下面是一些学习使用PHPUnit框架的基础知识和步骤

香港中文大学(深圳)吴保元教授课题组和浙江大学秦湛教授课题组联合发表了一篇后门防御领域的文章,已顺利被ICLR2022接收。近年来,后门问题受到人们的广泛关注。随着后门攻击的不断提出,提出针对一般化后门攻击的防御方法变得愈加困难。该论文提出了一个基于分割后门训练过程的后门防御方法。本文揭示了后门攻击就是一个将后门投影到特征空间的端到端监督训练方法。在此基础上,本文分割训练过程来避免后门攻击。该方法与其他后门防御方法进行了对比实验,证明了该方法的有效性。收录会议:ICLR2022文章链接:http

这个问题涉及到许多实际上并不了解核心技术并希望在SeleniumAutomation领域发展职业生涯的专业人士。编码这个术语让非程序员有点害怕,甚至不敢从自动化之类的东西开始。人们认为非程序员无法在自动化方面表现出色,但这只是在头脑中。许多值得和有能力的手动测试人员回避Selenium,只是认为它需要一些特殊技能。Selenium脚本是用多种语言设计的,例如Python、Ruby、C#、JavaScript和Java就是其中之一他们当中就有这样的人。了解了Java的受欢迎程度和未来前景,现在更倾

随着win10系统的成熟,微软停止win7的更新和支持,越来越多人选择win10系统使用,打算将自己的win7升级win10系统。不过很多小伙伴不知道win7如何升级win10系统,找不到升级的按键。下面小编教大家一个简单的win7升级win10系统的方法。我们可以借助工具轻松实现win7升级安装win10的方法,具体的操作步骤如下:1、先在电脑上下载安装小鱼一键重装系统工具并打开,关闭电脑的杀毒软件,备份c盘重要资料。然后选择需要安装的win10系统点击安装此系统。2、这个界面选择想要安装的软


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 English version
Recommended: Win version, supports code prompts!
