Abstract Classes in PHP

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Abstract class example in PHP
abstract class base
{
    // This is abstract function
    abstract function printdata();
      
    // This is not abstract function
    function pr()
    {
        echo "Base class";
    }
}
?>

chevron_right


Following are some important facts about abstract classes in PHP.

  • Like Java, PHP also an instance of abstract class can not be created.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    // Abstract class
    abstract 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();
    ?>

    chevron_right

    
    

    Output:

    Derived class
    
  • Like C++ or Java abstract class in PHP can contain constructor also.

    Example:



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    // Abstract class
    abstract class Base {
        function __construct() {
            echo "this is abstrct 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();
    ?>

    chevron_right

    
    

    Output:

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    // example to understand that an abstract 
    // class can not contain an mehtod with
    // body in php
    abstract class Base {
        abstract function printdata() {
            echo "Parent class printdata";
        }
    }
    ?>

    chevron_right

    
    

    Runtime Errors:

    PHP Fatal error:  Abstract function Base::printdata() cannot contain body 
    in /home/a7540402ade5337d505a779cf4797b38.php on line 7
    


My Personal Notes arrow_drop_up

All powers are within you You can do anything and everything Believe in that

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.