Abstract Class and Interface in PHP_PHP Tutorial
Abstract Class and Interface in PHP
I recently started learning PHP MySQL. Let’s record the key points in the learning process, and then consider writing a series of blogs on the process of developing the website.
This blog mainly introduces the difference between Abstract Class and Interface.
Abstract Class
What is Abstract Class
The same as the concept of abstract class in C, a class containing pure virtual function (called abstract method in Java and Php) is called Abstract Class. We sometimes call abstract Class base class, because base class cannot directly generate objects.
Abstract Class in PHP
Let’s look at the code:
abstract class abc { public function xyz() { return 1; } } $a = new abc();//this will throw error in php
The abstract class in PHP is the same as other oop languages. We use the keyword abstract to declare an abstract class. If you want to directly generate an object of this class, an error will be reported.
abstract class testParent { public function abc() { //body of your funciton } } class testChild extends testParent { public function xyz() { //body of your function } } $a = new testChild();
testChild inherits the abstract class testParent through the keyword extends, and then we can generate a testChild object.
Implement Abstract Method
Similar to pure virtual functions in C, we can only declare Abstract method in abstract classes, and we can only and must define it in subclasses.
Actually, this statement is not absolute, but for the convenience of memory, most textbooks say this. Let’s review the explanation of pure virtual functions in Effective C .
"Pure Virtual functions must be redeclared in the derived class, but they can also have their own implementation"
class Airplane{ public: virtual void fly(const Airport& destination) = 0; .... }; void Airplane::fly(const Airport& destination){ // 缺省行为,将飞机飞到指定的目的地 }
class ModelA: public Airplane{ public: virtual void fly(const Airport& destination) {Airplane::fly(destination);} .... }; class ModelB: public Airplane{ public: virtual void fly(const Airport& destination); .... }; void ModelB:: fly(const Airport& destination){ // 将C型飞机飞到指定的地方 }
The fly I want to be in is divided into two basic elements:
The declaration part represents the interface (which this derived class must use)
The definition part reflects the default behavior (that derived classes may use, but only if they explicitly request it)
The above content is excerpted from "Effective C 55 Specific Practices to Improve Programming and Design" Item 34: Distinguish between interface inheritance and implementation inheritance
Let’s come back and continue discussing the implementation of abstract method in PHP.
abstract class abc { abstract protected function f1($a , $b); } class xyz extends abc { protected function f1($name , $address) { echo $name , $address; } } $a = new xyz();
In abc, we declare an abstract method f1 using the keyword abstract. In PHP
Once you declare an abstract method in an abstract class, all subclasses that inherit this class must declare this method , otherwise, PHP will report an error.
abstract class parentTest { abstract protected function f1(); abstract public function f2(); //abstract private function f3(); //this will trhow error } class childTest { public function f1() { //body of your function } public function f2() { //body of your function } protected function f3() { //body of your function } } $a = new childTest();
As you can see from the above code, declaring a private abstract method will report an error because the private method can only be used in the current class.
Notice that the f1 function is protected in the abstract class, but we can declare it as public in the subclass. no any visibility is less restricted than public.
Interface
Interface in oop enforce definition of some set of method in the class.
Interface will force users to implement some methods. For example, if there is a class that requires set ID and Name attributes, then we can declare this class as an interface, so that all derived classes that inherit from this class will be forced to implement the setId and setName operations
Interface in php
Interface abc { public function xyz($b); }
Like other oop languages, we use the keyword Interface to declare it.
In this interface we declare a method xyz, thenAny time, such a method xyz
must be declared in the subclass
class test implements abc { public function xyz($b) { //your function body } }
You can use the keyword implements to inherit from interface.
In the interface, you can only use public, but not protected and private
interface template1 { public function f1(); } interface template2 extends template1 { public function f2(); } class abc implements template2 { public function f1() { //Your function body } public function f2() { //your function body } }
The template2 here will contain all the attributes of template1, so in the implements class abc of template2, you will have to implement function f1 and f2,
You can also extend multiple interfaces:
interface template1 { public function f1(); } interface template2 { public function f2(); } interface template3 extends template1, template2 { public function f3(); } class test implements template3 { public function f1() { //your function body } public function f2() { //your function body } public function f3() { //your function body } }
At the same time, your class can also implement multiple interfaces
interface template1 { public function f1(); } interface template2 { public function f2(); } class test implments template1, template2 { public function f1() { //your function body } public function f2() { //your function body } }
But if two interfaces contain methods with the same name, your class will not be able to implement them at the same time.
Methods inherited from interface must have the same parameter specifications . For example, the following code is feasible:
interface template1 { public function f1($a) } class test implements template1 { public function f1($a) { echo $a; } }
But code like this will error:
interface template1 { public function f1($a) } class test implements template1 { public function f1() { echo $a; } }
However, we do not need to name the parameters in the two methods with the same name. The following code is feasible:
interface template1 { public function f1($a) } class test implements template1 { public function f1($name) { echo $name; } }
同时,如果使用default value,你还可以改变参数的default value,下面的代码是可行的:
interface template1 { public function f1($a = 20) } class test implements template1 { public function f1($name = ankur) { echo $name; } }
Abstract Class和Interface之间的不同:
1. In abstract classes this is not necessary that every method should be abstract. But in interface every method is abstract.
在Abstract class中并非所有的method都必须是抽象的,但是在interface中所有的method都自动成为抽象的。就是在子类中必须声明和实现
2. Multiple and multilevel both type of inheritance is possible in interface. But single and multilevel inheritance is possible in abstract classes.
multiple和multilevel inheritance,我不知道改怎么翻译更好,multiple inheritance意思是 在interface中,一个class可以同时implements好多个interface;但是在abstract classes中,只能extends一个class。
当然你extends的这个class可能又extentds别的class,这就是所谓的multilevel inheritance。
3. Method of php interface must be public only. Method in abstract class in php could be public or protected both.
interface中的method必须是public的,但是在abstract class中可以是public或者protected。
4. In abstract class you can define as well as declare methods. But in interface you can only defined your methods.
在abstract class中你可以同时声明(declare)和定义(define)methodes,但是在interface中你只能定义那个methods

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











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.

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.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.
