What are php abstract classes and abstract methods

青灯夜游
Release: 2023-03-11 15:48:01
Original
2627 people have browsed it

Abstract method refers to a method without a method body, and the so-called no method body means that when declaring a method, there are no curly braces "{}" and its contents, but a semicolon is added directly after the method name. Finish. As long as a method in a class is an abstract method, then the class must be defined as an abstract class and needs to be modified using the "abstract" keyword.

What are php abstract classes and abstract methods

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In object-oriented language, a class can have One or more subclasses, and each class should have at least one public method as an entry point for external code to access it. Abstract classes and abstract methods are a concept introduced in PHP5, mainly to facilitate class inheritance.

1. Abstract method

Abstract method is a method without method body. The so-called no method body means that there are no curly braces when declaring the method { } and its content, but directly end with a semicolon after the method name. In addition, use the "abstract" keyword when declaring an abstract method. The format is as follows:

abstract 访问权限修饰符 function 方法名1(参数列表);
abstract 访问权限修饰符 function 方法名2(参数列表);
Copy after login

2. Abstract class

As long as there is an abstract method in a class, then this class must be defined as an abstract class, abstract Classes also need to be modified with the "abstract" keyword. Abstract classes can also contain member methods and member properties that are not abstract methods, but access permissions cannot be private (modified with the private keyword), because methods in abstract classes need Inherited by subclasses.

The following example demonstrates how to define an abstract class. The code is as follows:

abstract class 类名{
    public $name;
    abstract 访问权限修饰符 function 方法名1();
    abstract 访问权限修饰符 function 方法名2();
    访问权限修饰符 function 方法名3(){
        ... ... ;
    }
}
Copy after login

An abstract class is like a "semi-finished" class that contains unimplemented components. Abstract methods, so abstract classes cannot be instantiated, that is, objects cannot be created, and they cannot be used directly. Since an abstract class is a "semi-finished" class, what is the use of abstract classes?

You can think of an abstract class as defining a public interface for its subclasses, and handing over its operations (which may be part or all) to the subclasses for implementation. Abstract classes are used as templates for subclass overloading. Defining an abstract class is equivalent to defining a specification, which requires subclasses to comply.

When a subclass inherits an abstract class, it must implement the abstract methods in the abstract class according to the subclass's own needs. The subclass must implement all abstract methods in the parent class. Otherwise, there are still abstract methods in the subclass, so it is still an abstract class and cannot be instantiated as an object.

[Example] Define an abstract class, and then use another class to inherit this abstract class and implement the abstract methods in the abstract class.

<?php
    abstract class Website{
        public $name = &#39;PHP中文网<br>&#39;;
        public $url = &#39;https://www.php.cn/<br>&#39;;
        abstract function title();
        abstract function output();
    }
    class Demo extends Website{
        public function title(){
            echo &#39;抽象类和抽象方法&#39;;
        }
        public function output(){
            echo $this -> name.$this -> url;
        }
    }
    $obj = new Demo();
    $obj -> output();
    $obj -> title();
?>
Copy after login

The running results are as follows:

PHP中文网
https://www.php.cn/
抽象类和抽象方法
Copy after login

In addition, it should be noted that the access permissions of member methods in subclasses can be the same as those of abstract methods, but cannot be more strict. Moreover, the number of parameters of the member method in the subclass should be the same as the number of parameters of the abstract method.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What are php abstract classes and abstract methods. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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