


Analyze the three major characteristics of PHP object-oriented

class BenHang extends Card{ /*构造函数与及构造的继承*/ function __construct($cardno,$pwd, $name,$money){ parent::__construct($cardno,$pwd, $name,$money); } function take($money){ echo "本行取款{$money}没有手续费·····<br>"; }function zhuan($money){ echo "本行转账{$money}·····<br>"; } }$benhang=new BenHang(123,344,444,444);$benhang->check();$benhang->take(234);$benhang->zhuan(4555);/*其他银行卡的类*/class Qita extends Card{function __construct($cardno,$pwd, $name,$money){ parent::__construct($cardno,$pwd, $name,$money); } function take($money){ echo "非本行取款{$money}有手续费2元·····<br>"; } }$qita=new Qita(123,344,444,444);$qita->check();$qita->take(99);
PHP's three major characteristics: inheritance, packaging, polymorphism
1. How to implement inheritance?
Use the extends keyword for the subclass to inherit the parent class;
class Student extends Person{}# 2. Things to note when implementing inheritance?
① Subclasses can only inherit non-private properties of the parent class.
②After a subclass inherits a parent class, it is equivalent to copying the properties and methods of the parent class to the subclass, which can be called directly using $this.
③ PHP can only support single inheritance and does not support one class inheriting multiple classes. But a class carries out multi-level inheritance;
class Student extends Chengnian{}
//Student class has both Chengnian class and Person class Properties and methods
3. Method override (method rewriting)## Condition ① Subclass Inherit from parent class.
Condition ② The subclass overrides the existing method of the parent class.
Meeting the above two conditions is called method coverage. After overriding, when a subclass calls a method, the subclass's own method will be called. Similarly, in addition to method overrides, subclasses can also have attributes with the same name as the parent class for attribute overrides.
4. If the subclass overrides the parent class method, how to call the parent class method with the same name in the subclass?
partent::method name();
Therefore, when a subclass inherits a parent class, it needs to be The first step in the construction is to call the parent class constructor to copy.
function __construct($name,$sex,$school){
parent::__construct($name,$sex);
$this->school = $school;
}
实例一枚:
class Person{protected $name;public $sex; function __construct($name,$sex){ //声明构造函数 $this->name = $name; $this->sex = $sex; } function say(){ echo "我叫{$this->name},我是{$this->sex}生!<br>"; } } class Student extends Person{ //子类继承父类public $school; function __construct($name,$sex,$school){ //子类的构造函数 parent::__construct($name,$sex); //调用父类构造进行复制$this->school = $school; } function program(){ echo "PHP真好玩!我爱PHP!PHP是世界上最好用的编程语言!<br>"; } function say(){ parent::say(); //重写父类的同名方法echo "我是{$this->school}的"; } } $zhangsan = new Student("张三","男","起航"); $zhangsan->say(); $zhangsan->program();
二、封装
class Person{public $name;public $age;public $sex; function __construct($name, $age,$sex){ $this->name=$name; $this->setAge($age); $this->setSex($sex); } function setAge($age){if($age>=0&&$age<=120){return $this->age=$age; }else{ die("年龄输入有误!!!"); } } function setSex($sex){if($sex=="女"||$sex=="男"){return $this->sex=$sex; }else{ die("性别输入有误!!!"); } } function say(){ echo "我的名字叫{$this->name},我的年龄{$this->age},我的性别是{$this->sex}<br>"; } }class Work extends Person{private $position; function __construct($name, $age,$sex,$position){ parent::__construct($name, $age,$sex); $this->job=$job; $this->setPosition($position); } function setPosition($position){ $arr=['总监','董事长','程序员','清洁工'];if(in_array($position, $arr)){return $this->position=$position; }else{ die("不存在该职位"); } } function __set($key,$value){if($key=="age"){return parent::setAge($value); } elseif($key=="sex"){return parent::setSex($value); } elseif($key=="position"){return $this->setPosition($value); }return $this->$key=$value; } function say(){ parent::say(); echo "我的职位是{$this->position}"; } } $zhangsan=new Work("张三",22,"男","总监"); $zhangsan->setSex("女"); $zhangsan->setAge(30);// $zhangsan->setPosition("董事长");$zhangsan->position="董事长"; $zhangsan->name="lisi";$zhangsan->say();
/*墨盒接口 * 纸张接口*/ interface InkBox{ function color(); }interface Paper{ function sizes(); }class Computer{function fangfa(InkBox $a,Paper $b){ //父类引用echo "即将开始打印····<br>"; $a->color();$b->sizes();echo "打印结束···<br>"; } }class Color implements InkBox{function color(){echo "正在装载彩色墨盒<br>";echo "实现彩色墨盒<br>"; } }class White implements InkBox{function color(){echo "正在装载黑白墨盒<br>"; echo "实现黑白墨盒<br>"; } }class A4 implements Paper{function sizes(){echo "正在加载A4纸张<br>";echo "实现A4纸张<br>"; } }class A5 implements Paper{function sizes(){echo "实现A5纸张<br>"; } }$com=new Computer();//创建对象$com->fangfa(new Color(),new A4());//子类对象
The above is the detailed content of Analyze the three major characteristics of PHP object-oriented. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
