1.快速入门案例:
案例1:
<?php//定义规范/方法 interface Iusb { public function start(); public function stop(); } //编写手机类,让它去实现接口 //当一个类实现了某个接口,则要求该类必须实现这个接口的所有方法 class Camera implements Iusb { public function start() { echo "相机开始工作"; } public function stop() { echo "相机停止工作"; } } class Phone implements Iusb { public function start() { echo "手机开始工作"; } public function stop() { echo "手机停止工作"; } } //如何使用 $camera1=
new Camera(); $camera1->start(); $phone1=new Phone(); $phone1->start();?>
效果:
相机开始工作手机开始工作
来自
2.接口使用的基本语法:
interface接口名
{
//属性
//方法
}
->接口中的方法没有方法体
->如何实现接口
Class 类名implements 接口名
{
}
**可以实现多个接口
Class 类名implements 接口名1,接口名2……
{
}
->接口的作用:声明一些方法,供其它类来实现,接口还体现编程中我们希望的效果:高内聚低耦合 的特点 ,见图1
深入讨论-接口interface
什么时候用接口?
1.定规范
2.定下规范,让别的程序人来实现
3.当多个类,它们之间平级的关系,这些类都要去实习某个功能,只是实现方式不一样
细节-interface
1.接口不能实例化;
2.接口中所有方法都不能有方法体;
3.一个类可以实现多个接口,但以","隔开;
4.接口中可以有属性(区别于方法),但必须为常量,并且是public
案例2:
<?php interface IsUSB { const A=555;//常量前面不需要$ } class Test implements IsUSB{ public function aa() { echo "<br>继承的常量为:".IsUSB::A;//常量引用方法:接口名::常量名称 }}$test1=new Test();$test1->aa(); echo "<br>没有继承的常量为:".IsUSB::A;//常量引用方法:接口名::常量名称?>
5.接口的方法都是public的,即没有protected,以及private,如果不写,默认为public;
6.接口不能继承其它类,但可以继承其它接口;并且一个接口可以继承多个接口,而一个类不能继承多个类;一个类可以同时继承某个类同时实现接口;
例如:inerface 接口名 extends 接口名1,接口名2…… 如图2
案例3:
<?php interface IsUSB01 { public function a(); } interface IsUSB02 { public function b(); } interface IsUSB03 extends IsUSB01,IsUSB02{ }Class Class1{ public function TST() { echo "继承类名为Class1<br>"; }} class Test extends Class1 implements IsUSB03 { public function __construct() { echo "接口IsUSB03继承了接口IsUSB01以及接口IsUSB02<br>";
} public function a() { echo "实现接口IsUSB01的a()<br>"; } public function b() { echo "实现接口IsUSB02的b()<br>"; } }$test1=new Test();$test1->a();$test1->b();$test1->TST(); ?>
结果:
接口IsUSB03继承了接口IsUSB01以及接口IsUSB02
实现接口IsUSB01的a()
实现接口IsUSB02的b()
继承类名为Class1
来自
其中,接口IsUSB03继承了IsUSB01以及IsUSB02,因为是接口继承接口,不需要实现方法,而,Test类引用了IsUSB03接口,则需要实现方法。
3.继承与接口区别
实现接口可以看作,对单一继承类的一种补充,同时,继承是层次式的,不太灵活,多层次继承如果修改某个类,就会打破继承的平衡,而接口没有那么麻烦,较为灵活

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

In PHP, trait is suitable for situations where method reuse is required but not suitable for inheritance. 1) Trait allows multiplexing methods in classes to avoid multiple inheritance complexity. 2) When using trait, you need to pay attention to method conflicts, which can be resolved through the alternative and as keywords. 3) Overuse of trait should be avoided and its single responsibility should be maintained to optimize performance and improve code maintainability.

Dependency Injection Container (DIC) is a tool that manages and provides object dependencies for use in PHP projects. The main benefits of DIC include: 1. Decoupling, making components independent, and the code is easy to maintain and test; 2. Flexibility, easy to replace or modify dependencies; 3. Testability, convenient for injecting mock objects for unit testing.

SplFixedArray is a fixed-size array in PHP, suitable for scenarios where high performance and low memory usage are required. 1) It needs to specify the size when creating to avoid the overhead caused by dynamic adjustment. 2) Based on C language array, directly operates memory and fast access speed. 3) Suitable for large-scale data processing and memory-sensitive environments, but it needs to be used with caution because its size is fixed.

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.

In JavaScript, you can use NullCoalescingOperator(??) and NullCoalescingAssignmentOperator(??=). 1.??Returns the first non-null or non-undefined operand. 2.??= Assign the variable to the value of the right operand, but only if the variable is null or undefined. These operators simplify code logic, improve readability and performance.


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.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.