Home Backend Development PHP Tutorial A brief analysis of the concepts and differences between abstract classes and interfaces in PHP_PHP Tutorial

A brief analysis of the concepts and differences between abstract classes and interfaces in PHP_PHP Tutorial

Jul 21, 2016 pm 03:03 PM
abstract php code the difference copy definition abstract interface concept of kind

Copy code The code is as follows:

//Definition of abstract class:
abstract class ku{ //Define an abstract class
abstract function kx();
......
}
function aa extends ku{
//Method to implement abstract class
function kx(){
echo 'sdsf';
}
}
//Usage method
$aa=new aa;
$aa->kx();
//1. Define some Methods, subclasses must fully implement all methods in this abstraction
//2. Objects cannot be created from abstract classes, its meaning is to be extended
//3. Abstract classes usually have abstract methods, and there are no Braces
//4. Abstract methods do not have to implement specific functions and are completed by subclasses
//5. When a subclass implements a method of an abstract class, the visibility of the subclass must be greater than or equal to abstract Definition of method
//6. The method of abstract class can have parameters or be empty
//7. If the abstract method has parameters, then the implementation of the subclass must also have the same number of parameters
////////////////////////////////Definition of interface class:
interface Shop{
public function buy($ gid);
public function sell($gid);
abstract function view($gid);
}
//If you want to use an interface, you must define all methods in the interface class with one less method No (except abstract).
//In this way, no matter how others do the following method in a large project, they must implement all the methods in this interface!
//Example: A method to implement the above interface
class BaseShop implements Shop{
public function buy($gid){
echo 'You purchased the product with ID:' . $gid . ';
}
public function sell( $gid){
echo 'You purchased and sold products with the ID:' . $gid . ';
}
public function view($gid){
echo 'You browsed the products with the ID :' . $gid . 'Product';
}
}
//Multiple inheritance example of interface:
interface staff_i1{ //Interface 1
function setID();
function getID();
}
interface staff_i2{ //Interface 2
function setName();
function getName();
}
class staff implements staff_i1,staff_i2{
private $id;
private $name;
function setID($id){
$this->id = $id;
}
function getID(){
return $this->id;
}
function setName($name){
$this->name = $name;
}
function getName(){
return $this->name;
}
function otherFunc(){ //This is a method that does not exist in the interface
echo “Test”;
}
}
?>

Their differences:
1. Abstract classes can have non-abstract methods, while interfaces can only have abstract methods!
2. A class can inherit multiple interfaces, but a class can only inherit one abstract class!
3. The interface is used through the implements keyword, and the abstract class is used by inheriting the extends keyword!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327877.htmlTechArticleCopy the code The code is as follows: //Definition of abstract class: abstract class ku{ //Define an abstract class abstract function kx(); ...... } function aa extends ku{ //Implement the method of abstract class fu...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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 difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

What is the function of C language sum? What is the function of C language sum? Apr 03, 2025 pm 02:21 PM

There is no built-in sum function in C language, so it needs to be written by yourself. Sum can be achieved by traversing the array and accumulating elements: Loop version: Sum is calculated using for loop and array length. Pointer version: Use pointers to point to array elements, and efficient summing is achieved through self-increment pointers. Dynamically allocate array version: Dynamically allocate arrays and manage memory yourself, ensuring that allocated memory is freed to prevent memory leaks.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

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.

The difference between char and wchar_t in C language The difference between char and wchar_t in C language Apr 03, 2025 pm 03:09 PM

In C language, the main difference between char and wchar_t is character encoding: char uses ASCII or extends ASCII, wchar_t uses Unicode; char takes up 1-2 bytes, wchar_t takes up 2-4 bytes; char is suitable for English text, wchar_t is suitable for multilingual text; char is widely supported, wchar_t depends on whether the compiler and operating system support Unicode; char is limited in character range, wchar_t has a larger character range, and special functions are used for arithmetic operations.

Which is better PHP or Laravel? Which is better PHP or Laravel? Mar 27, 2025 pm 05:31 PM

PHP and Laravel are not directly comparable, because Laravel is a PHP-based framework. 1.PHP is suitable for small projects or rapid prototyping because it is simple and direct. 2. Laravel is suitable for large projects or efficient development because it provides rich functions and tools, but has a steep learning curve and may not be as good as pure PHP.

Is there any mobile app that can convert XML into PDF? Is there any mobile app that can convert XML into PDF? Apr 02, 2025 pm 08:54 PM

An application that converts XML directly to PDF cannot be found because they are two fundamentally different formats. XML is used to store data, while PDF is used to display documents. To complete the transformation, you can use programming languages ​​and libraries such as Python and ReportLab to parse XML data and generate PDF documents.

See all articles