Abstract Classes in PHP - GeeksforGeeks

19 Jul, 2019
Abstract classes are the classes in which at least one method is abstract. Unlike C++ abstract classes in PHP are declared with the help of abstract keyword. Use of abstract classes are that all base classes implementing this class should give implementation of abstract methods declared in parent class. An abstract class can contain abstract as well as non abstract methods.
<?php  // Abstract class example in PHPabstract class base{    // This is abstract function    abstract function printdata();          // This is not abstract function    function pr()    {        echo "Base class";    }}?>
Following are some important facts about abstract classes in PHP.
Like Java, PHP also an instance of abstract class can not be created.Example:<?php  // Abstract classabstract class Base {    // This is abstract function    abstract function printdata();}class Derived extends base {    function printdata() {        echo "Derived class";    }}  // Uncommenting the following line will // cause compiler error as the line tries// to create an instance of abstract class. // $b = new Base();       $b1 = new Derived;$b1->printdata();?>Output:Derived class

Example:
<?php  // Abstract classabstract class Base {    // This is abstract function    abstract function printdata();}class Derived extends base {    function printdata() {        echo "Derived class";    }}  // Uncommenting the following line will // cause compiler error as the line tries// to create an instance of abstract class. // $b = new Base();       $b1 = new Derived;$b1->printdata();?>
Derived class

Like C++ or Java abstract class in PHP can contain constructor also.Example:<?php  // Abstract classabstract class Base {    function __construct() {        echo "this is abstract class constructor ";    }      // This is abstract function    abstract function printdata();}class Derived extends base {    function __construct() {        echo "\n Derived class constructor";    }    function printdata() {        echo "\n Derived class printdata function";    }}$b1 = new Derived;$b1->printdata();?>Output:Derived class constructor
 Derived class printdata function

Example:
<?php  // Abstract classabstract class Base {    function __construct() {        echo "this is abstract class constructor ";    }      // This is abstract function    abstract function printdata();}class Derived extends base {    function __construct() {        echo "\n Derived class constructor";    }    function printdata() {        echo "\n Derived class printdata function";    }}$b1 = new Derived;$b1->printdata();?>
Derived class constructor
 Derived class printdata function

Unlike Java, an abstract class can not be created which does not contains at least one abstract method in PHP. If we run the following example then it will display an error message.Example:<?php  // example to understand that an abstract // class can not contain an method with// body in phpabstract class Base {    abstract function printdata() {        echo "Parent class printdata";    }}?>Runtime Errors:PHP Fatal error:  Abstract function Base::printdata() cannot contain body 
in /home/a7540402ade5337d505a779cf4797b38.php on line 7

Example:
<?php  // example to understand that an abstract // class can not contain an method with// body in phpabstract class Base {    abstract function printdata() {        echo "Parent class printdata";    }}?>
Runtime Errors:
PHP Fatal error:  Abstract function Base::printdata() cannot contain body 
in /home/a7540402ade5337d505a779cf4797b38.php on line 7

Shivam_k
PHP-OOP
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Insert Form Data into Database using PHP ?
How to execute PHP code using command line ?
How to pop an alert message box using PHP ?
PHP in_array() Function
How to convert array to string in PHP ?
Top 10 Front End Developer Skills That You Need in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?