Detailed explanation of php abstract methods and abstract class instances

墨辰丷
Release: 2023-03-28 11:44:01
Original
1731 people have browsed it

This article mainly introduces PHP abstract methods and abstract classes, and analyzes the concepts, functions, definitions and usage of PHP abstract methods and abstract classes in the form of examples. Friends in need can refer to the following

for details As follows:

What is an abstract method?

A method defined in a class without a method body is an abstract method. When the method is declared, there are no curly braces and the content within it. In addition, when declaring an abstract method, keywords must be added. abstract to modify.

For example:

abstract function fun1();
abstract function fun2();
Copy after login

As long as there is an abstract method in a class, then this class must be defined as an abstract class.

Abstract classes should also be modified with abstract.

Abstract classes can have methods and member attributes that are not abstract.

But as long as there is an abstract method, this class must be defined as an abstract class.

How to use abstract classes? The most important point is that abstract classes cannot produce instance objects!

Defining an abstract class is equivalent to defining a specification! This specification requires subclasses to comply! After a subclass inherits an abstract class, it implements the abstract methods in the abstract class according to the needs of the subclass.

The subclass must implement all the abstract methods in the abstract class, otherwise there are still abstract methods in the subclass, the subclass is still an abstract class, and it still cannot be instantiated!

<?php
abstract class Demo{
var $test;
abstract function fun1();
abstract function fun2();
function fun3(){
    ...
  }
}
class Test extends Demo{
function fun1(){
   ...
}
function fun2(){
   ...
}
}
$test = new Test();
?>
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHPRealize multi-process and multi-thread

PHPSome basic tags to learn

##PHP5 ways to obtain user access IP address

The above is the detailed content of Detailed explanation of php abstract methods and abstract class instances. 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