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











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.
