Home Backend Development PHP Tutorial In-depth interpretation of the problem analysis of abstract classes and abstract methods in PHP_PHP Tutorial

In-depth interpretation of the problem analysis of abstract classes and abstract methods in PHP_PHP Tutorial

Jul 13, 2016 am 10:42 AM
abstract oop php about analyze exist object abstract method go deep of Interpretation question For

In object-oriented (OOP) languages, a class can have one or more subclasses, and each class has at least one public method as an interface for external code to access. Abstract methods are introduced to facilitate inheritance. Now let’s take a look at how abstract classes and abstract methods are defined and their characteristics.

What is an abstract method? The method we define in the class with only the method name and no method body is an abstract method. The so-called no method body means that there is no curly braces and the content in the method when it is declared. Instead, when it is declared directly, a semicolon is added after the method name. , In addition, when declaring an abstract method, add a keyword "abstract" to modify it.


1. Abstract keyword: abstract

Abstract means that it cannot be explained exactly, but it has a certain concept or name. To declare an abstract class or method in PHP, we need to use the abstract keyword.

2. Definition of abstract methods and abstract classes

At least one method in a class is abstract, we call it an abstract class. So if you define an abstract class, first define the abstract method.

Copy code The code is as follows:

abstract class class1{                                                 abstract function fun1();

......
}

1. There is at least one abstract method in the class
2. Abstract methods are not allowed to have { }
3. Abstract
must be added before the abstract method

3. Rules for using abstract classes and methods

Several characteristics of abstract classes:

1. It cannot be instantiated, but can only be inherited

2. In inherited derived classes, all abstract methods must be overloaded before they can be instantiated

The statement about abstract methods is as follows:

Copy code The code is as follows:
abstract function fun1();

?>


What is an abstract class? As long as a method in a class is an abstract method, then the class must be defined as an abstract class. Abstract classes must also be modified with the keyword "abstract". Abstract classes cannot instantiate objects, so abstract methods are used as templates for subclass method overloading, and all methods in the inherited abstract class must be implemented.

Abstract classes and implementation examples of abstract classes are as follows:

Copy code The code is as follows:
abstract class User{ //Define abstract class
abstract protected function getUser(); //Define abstract method
public function print_content(){
print $this->getUser();
}
}
class vipUser extends User{

protected function getUser(){
return "Abstract class and abstract method www.jb51.net";
}
}

$user=new vipUser(); //Instantiate subclass

$user->print_content(); //Abstract class and abstract method
?>

Note: When an abstract class inherits another abstract class (the purpose is to extend the abstract class), it cannot override the abstract method of the parent class.

In PHP5.1, static abstract methods are supported in abstract classes. In the example below, we see that static abstract methods can be declared. When implementing this method, it must be a static method.

Copy code The code is as follows:
abstract class User{
protected static $sal =0;
static abstract function getSal();
static abstract function setSal($sal);
}
class VipUser extends User{
static function getSal(){
return self::$sal;
}
static function setSal($sal){
self::$sal=$sal;
}
}
VipUser::setSal(100 );
echo "you sal is www.jb51.net " . VipUser::getSal();
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/635836.htmlTechArticleIn object-oriented (OOP) languages, a class can have one or more subclasses, and each class There is at least one public method that serves as an interface for external code to access. And abstract methods are for...
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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles