Home > Backend Development > PHP Tutorial > PHP class abstraction

PHP class abstraction

WBOY
Release: 2023-08-31 06:00:01
forward
1544 people have browsed it

PHP class abstraction

Introduction

In object oriented programming, an abstract class is the one that can be instantiated, i.e. it is not possible to declare object of such class. PHP supports concept of abstarct class since version 5.0

A class defined with abstract keyword becomes an abstract class. Further, any class which contains at least one abstract method is also considered abstract.

Syntax

<?php
class testclass{
   //
}
?>
Copy after login

如果我们尝试创建这个类的一个对象,PHP解析器会抛出以下错误:

$a=new testclass();
PHP Fatal error: Uncaught Error: Cannot instantiate abstract class testclass
Copy after login

abstract method

Abstract method only declares its signature i.e. its visibility, arguments and return type with type hints and donot have any functionality. A class that inherits such an abstract class must override (provide definition) all abstract methods. Corresponding method in child class must carry same signature as in parent class. If child class doesn't fulfil this condition, PHP parser throws exception. A class that extends an abstract class can now be instantiated, hence it is called concrete class

In following example, parent class has two abstract methods, only one of which is redefined in child class. This results in error as follows −

Example

 Live Demo

<?php
abstract class testclass{
   abstract function test1();
   abstract function hello();
}
class myclass extends testclass{
   function test1(){
      echo "Overrides parent test method";
   }
}
$a=new myclass();
?>
Copy after login

Output

Following is the error message

PHP Fatal error: Class myclass contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (testclass::hello)
Copy after login

Abstract method with arguments

When abstract method is defined with arguments, it must be overridden in child class with same number of arguments

In following example, abstract method in parent class has two arguments. Child class also defines same function with two arguments

Example

 Live Demo

<?php
abstract class testclass{
   abstract function hello($name, $age);
}
class myclass extends testclass{
   function hello($name, $age){
      echo "My name is $name and my age is $age";
   }
}
$a=new myclass();
$a->hello("Ravi",20);
?>
Copy after login

输出

这将产生以下输出 −

My name is Ravi and my age is 20
Copy after login

The above is the detailed content of PHP class abstraction. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
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