• home
  • forum
  • my
  • kt
  • download
  • Abstract classes in PHP

    Author: 2008-08-19 09:24:52 From:

    Before reading this article I assume you know basic OOP, including inheritance and encapsulation.

    Also, please note that abstract classes (in way way a form of encapsulation) is only possible since PHP5.

    This article is about Abstract classes. You've read a small introduction in the description, although some people won't agree with me saying it's always a parent class. You will figure out why in just a few minutes.

    When defining a new class with a subclass it's simple and very loose. You can do whatever you want in the subclass, without many restrictions from above. With an abstract class you can define some rules for the subclass.

    Lets say we'll have the following setup:

    php
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15

    <?php
     
    class Bike {
     
       // Main bike functions
     
    }
     
    class Kawasaki extends Bike {
     
       // Some function specially for Kawasaki bikes
     
    }
     
    ?>
     


    Now visualise we would add the following subclasses: Honda, Ducati and MV Agusta. That will leave us with 4 subclasses. We can now fill each subclass with all desired function that's specific to each bike.

    But something happens, we need to know the (factory) favorite colour of all biks, we can do that by making functions for all subclasses:

    php
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17

    <?php
     
    class Bike {
     
       // Main bike functions
     
    }
     
    class Kawasaki extends Bike {
     
       public function getStockColor() {
          return "green";
       }
     
    }
     
    ?>
     


    That will work perfect for all subclasses. But imagine you working on these classes quite much and you have a huge system. A system where you cannot remember if you have the getStockColor function in each bike subclass.

    Lets say you forgot to implement the function in the Ducati subclass and this has the result that your whole application is screwed, all Ducati pages parse with errors.

    Of course this is a lame example, but this little fault is made quite often.

    This is where the Abstract class kicks in. In the abstract class we can make sure all our subclasses have the methods we want. Take a look at the following example of our Bike class.

    php
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21

    <?php
     
    abstract class Bike {
     
       // Main bike functions
     
     
       // Add an abstract function
       abstract function getStockColor();
     
    }
     
    class Kawasaki extends Bike {
     
       // The subclass
     
    }
     
    $myBike = new Kawasaki;
     
    ?>
     


    I've turned the Bike class into an abstract class and added the getStockColor function as an abstract function (method) to the class.

    When defining a abstract function in an abstract class, all subclasses of the parent class have to implement the abstract function. Or else it will result in a fatal error:

    Fatal error: Class Kawasaki contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Bike::getStockColor) in /var/vhost/jim.zonax.net/index.php on line 17

    Now add the function to the subclass:

    php
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24

    <?php
     
    abstract class Bike {
     
       // Main bike functions
     
     
       // Add an abstract function
       abstract function getStockColor();
     
    }
     
    class Kawasaki extends Bike {
     
       function getStockColor() {
          return "Green";
       }
     
    }
     
    $myBike = new Kawasaki;
    echo $myBike->getStockColor();
     
    ?>
     


    This will output:

    Green

    So using the abstract classes will make sure you have the required functions in all subclasses, you can just expect all subclasses have that function because it won't even parse if that would not be the case.

    And don't worry about your basic functions in your main class. As long as they are not abstract they can function as proper functions:

    php
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24

    <?php
     
    abstract class Bike {
     
       public function __construct() {
          echo "wroem";
       }
     
       abstract function getStockColor();
     
    }
     
    class Kawasaki extends Bike {
     
       function getStockColor() {
          return "Green";
       }
     
    }
     
    $myBike = new Kawasaki;
    echo $myBike->getStockColor();
     
    ?>
     


    Next time I will talk about interfaces, very similar to abstract classes but in the end also very different.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (9)
      Link Indexing (6)
      Mailing List Management (8)
      Miscellaneous (10)
      Networking (6)
      News Publishing (6)
      OOP (22)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (6)
      Postcards (1)
      Randomizing (14)
      Redirection (11)
      Searching (9)
      Security (29)
      Site Navigation (16)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot