What is an abstract class in php? how to use?

王林
Release: 2023-02-23 17:14:01
forward
3408 people have browsed it

What is an abstract class in php? how to use?

Abstract class: At least one method in a class is abstract, we call it an abstract class.

Requirements:

1. There must be at least one abstract method in a class

2. Abstract methods are not allowed to have {}

3. Abstract

must be added in front of the abstract method. Example: Adding abstract

<?php 
    abstract class Human {
        public abstract function getInfo () {
            echo &#39;我是getInfo&#39;;
        }
    }
?>
Copy after login

in front of the class defines an abstract class of Human. There is an abstract method in the abstract class. When executed, an error is reported.

Fatal error: Abstract function Human::getInfo() cannot contain body
Copy after login

Abstract methods cannot have a text part (no method body), and the curly braces need to be removed

<?php 
    abstract class Human {
        public abstract function getUserInfo ();
        public abstract function getWalletInfo ();
    }
    
    class Student extends Human {
        public function getUserInfo () {
            echo &#39;getinfo&#39;;
        }
        
        public function getWalletInfo () {
            echo &#39;getwalletInfo&#39;;
        }
    }
    
    $Tom = new Student();
    $Tom -> getUserInfo();
    $Tom -> getWalletInfo ();
?>
Copy after login

Note:

1. Abstract classes cannot be Instantiated, can only be inherited.
2. In the inherited derived class, all abstract methods must be overloaded before instantiation

abstract class Human {
    public abstract function getUserInfo ();
    public abstract function getWalletInfo ();
}

class Student extends Human {
    public function getUserInfo () {
        echo &#39;getinfo&#39;;
    }
    
    // public function getWalletInfo () {
    //     echo &#39;getwalletInfo&#39;;
    // }
}

$Tom = new Student();
Copy after login

For example, if the derived class lacks a getWalletInfo() method, new will fail

The meaning of abstract class: When a derived class inherits an abstract class, it must use the naming rules of the abstract class to establish methods, otherwise the derived class is not allowed to be instantiated. In fact, it declares a specification to achieve The purpose of normative methods.

If you want to know more related content, please visit the php Chinese website: PHP video tutorial

The above is the detailed content of What is an abstract class in php? how to use?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template