How to use abstract classes and interfaces in PHP (code)
This article introduces you to the article content about the usage (code) of abstract classes and interfaces in PHP. It has a good reference value and I hope it can help friends in need.
<?php /*** ====笔记部分==== 接口的具体语法: 0:以人类为, class Human 是人的草图 而接口 是零件 可以用多种零件组合出一种新特种来. 1: 如上,接口本身即是抽象的,内部声明的方法 默认也是抽象的. 不用加 abstract 2: 一个类可以一次性实现多个接口. 语法用 implements 实现 (把我这几个功能实现了) class ClassName implements interface1,interface2,interface3 { } 然后再把接口的功能给实现. 3: 接口也可以继承, 用extends 4:接口是一堆方法的说明,不能加属性 5:接口就是供组装成类用的,封闭起来没有意义,因此方法只能是public ***/ interface animal { protected function eat(); } interface monkey extends animal { public function run(); public function cry(); } interface wisdom { public function think(); } interface bird extends animal{ public function fly(); } /* // 下面有误,monkey继承的aniaml接口,因此必须要把eat()实现 class Human implements monkey,wisdom { public function run() { echo '走'; } public function cry() { echo '哭'; } public function think() { echo '思考'; } } */ class Human implements monkey,wisdom { public function eat() { echo '吃'; } public function run() { echo '走'; } public function cry() { echo '哭'; } public function think() { echo '思考'; } } ?> <?php //04.php /*** ====笔记部分==== 面向对象的一个观点: 做的越多,越容易犯错 抽象类{就定义类模板}--具体子类实现{china,japan,english} 接口: ***/ // 抽象的数据库类 /* 创业做网站 到底用什么数据库? mysql, oracle,sqlserver,postgresql? 这样:先开发网站,运行再说. 先弄个mysql开发着,正式上线了再数据库也不迟 引来问题: 换数据库,会不会以前的代码又得重写? 答:不必,用抽象类 开发者,开发时,就以db抽象类来开发. */ abstract class db { public abstract function connect($h,$u,$p); public abstract function query($sql); public abstract function close(); } /* // 下面这个代码有误 // 因为子类实现时, connect和抽象类的connect参数不一致 class mysql extends db { public function connect($h,$h) { return true; } public function query($sql,$conn) { } public function close() { } } */ /* 下面这个mysql类,严格实现了db抽象类 试想: 不管上线时,真正用什么数据库 我只需要再写一份如下类 class oracle extends db { } class mssql extends db { } class postsql extends db { } 业务逻辑层不用改? 为什么不用改? 因为都实现的db抽象类. 我开发时,调用方法不清楚的地方,我就可以参考db抽象类. 反正子类都是严格实现的抽象类. */ class mysql extends db { public function connect($h,$h,$u) { return true; } public function query($sql) { } public function close() { } } /* 接口 就更加抽象了 比如一个社交网站, 关于用户的处理是核心应用. 登陆 退出 写信 看信 招呼 更换心情 吃饭 骂人 捣乱 示爱 撩骚 这么多的方法,都是用户的方法, 自然可以写一个user类,全包装起来 但是,分析用户一次性使不了这么方法 用户信息类:{登陆,写信,看信,招呼,更换心情,退出} 用户娱乐类:{登陆,骂人,捣乱,示爱,撩骚,退出} 开发网站前,分析出来这么多方法, 但是,不能都装在一个类里, 分成了2个类,甚至更多. 作用应用逻辑的开发,这么多的类,这么多的方法,都晕了. */ interface UserBase { public function login($u,$p); public function logout(); } interface UserMsg { public function wirteMsg($to,$title,$content); public function readMsg($from,$title); } interface UserFun { public function spit($to); public function showLove($to); } /* 作为调用者, 我不需要了解你的用户信息类,用户娱乐类, 我就可以知道如何调用这两个类 因为: 这两个类 都要实现 上述接口. 通过这个接口,就可以规范开发. */ /* 下面这个类,和接口声明的参数不一样,就报错, 这样,接口强制统一了类的功能 不管你有几个类,一个类中有几个方法 我只知道,方法都是实现的接口的方法. */ class User implements UserBase { public function login($u) { } } ?>
Interface syntax:
<?php /*** ====笔记部分==== 接口的具体语法: 0:以人类为, class Human 是人的草图 而接口 是零件 可以用多种零件组合出一种新特种来. 1: 如上,接口本身即是抽象的,内部声明的方法 默认也是抽象的. 不用加 abstract 2: 一个类可以一次性实现多个接口. 语法用 implements 实现 (把我这几个功能实现了) class ClassName implements interface1,interface2,interface3 { } 然后再把接口的功能给实现. 3: 接口也可以继承, 用extends 4:接口是一堆方法的说明,不能加属性 5:接口就是供组装成类用的,封闭起来没有意义,因此方法只能是public ***/ interface animal { protected function eat(); } interface monkey extends animal { public function run(); public function cry(); } interface wisdom { public function think(); } interface bird extends animal{ public function fly(); } /* // 下面有误,monkey继承的aniaml接口,因此必须要把eat()实现 class Human implements monkey,wisdom { public function run() { echo '走'; } public function cry() { echo '哭'; } public function think() { echo '思考'; } } */ class Human implements monkey,wisdom { public function eat() { echo '吃'; } public function run() { echo '走'; } public function cry() { echo '哭'; } public function think() { echo '思考'; } } ?> <?php //04.php /*** ====笔记部分==== 面向对象的一个观点: 做的越多,越容易犯错 抽象类{就定义类模板}--具体子类实现{china,japan,english} 接口: ***/ // 抽象的数据库类 /* 创业做网站 到底用什么数据库? mysql, oracle,sqlserver,postgresql? 这样:先开发网站,运行再说. 先弄个mysql开发着,正式上线了再数据库也不迟 引来问题: 换数据库,会不会以前的代码又得重写? 答:不必,用抽象类 开发者,开发时,就以db抽象类来开发. */ abstract class db { public abstract function connect($h,$u,$p); public abstract function query($sql); public abstract function close(); } /* // 下面这个代码有误 // 因为子类实现时, connect和抽象类的connect参数不一致 class mysql extends db { public function connect($h,$h) { return true; } public function query($sql,$conn) { } public function close() { } } */ /* 下面这个mysql类,严格实现了db抽象类 试想: 不管上线时,真正用什么数据库 我只需要再写一份如下类 class oracle extends db { } class mssql extends db { } class postsql extends db { } 业务逻辑层不用改? 为什么不用改? 因为都实现的db抽象类. 我开发时,调用方法不清楚的地方,我就可以参考db抽象类. 反正子类都是严格实现的抽象类. */ class mysql extends db { public function connect($h,$h,$u) { return true; } public function query($sql) { } public function close() { } } /* 接口 就更加抽象了 比如一个社交网站, 关于用户的处理是核心应用. 登陆 退出 写信 看信 招呼 更换心情 吃饭 骂人 捣乱 示爱 撩骚 这么多的方法,都是用户的方法, 自然可以写一个user类,全包装起来 但是,分析用户一次性使不了这么方法 用户信息类:{登陆,写信,看信,招呼,更换心情,退出} 用户娱乐类:{登陆,骂人,捣乱,示爱,撩骚,退出} 开发网站前,分析出来这么多方法, 但是,不能都装在一个类里, 分成了2个类,甚至更多. 作用应用逻辑的开发,这么多的类,这么多的方法,都晕了. */ interface UserBase { public function login($u,$p); public function logout(); } interface UserMsg { public function wirteMsg($to,$title,$content); public function readMsg($from,$title); } interface UserFun { public function spit($to); public function showLove($to); } /* 作为调用者, 我不需要了解你的用户信息类,用户娱乐类, 我就可以知道如何调用这两个类 因为: 这两个类 都要实现 上述接口. 通过这个接口,就可以规范开发. */ /* 下面这个类,和接口声明的参数不一样,就报错, 这样,接口强制统一了类的功能 不管你有几个类,一个类中有几个方法 我只知道,方法都是实现的接口的方法. */ class User implements UserBase { public function login($u) { } } ?>
The above is the detailed content of How to use abstract classes and interfaces in PHP (code). 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

Alipay PHP...

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

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,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

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.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
