Abstraction and Interface in PHP

Abstraction:

In inheritance, we see that child class is taking properties from parent class and child class is independent of parent class. Sometimes it's good to be able to lay down some ground rules about how a child class should behave. Abstract class and functions do this work. An abstract class contains some incomplete (abstract) function(s) which child class has to fulfilled for being a whole class otherwise child class will also be an abstract class.

  1. <?php
  2. // defining an abstract class
  3. abstract class AbsClass
  4. {
  5. function __construct()
  6. {
  7. echo "this is an abstract class <br>";
  8. }
  9. // abstract function
  10. abstract public function abs_function();
  11. function full_function()
  12. {
  13. echo "this is not an abstract function <br>";
  14. }
  15. }
  16. class SubClass extends AbsClass
  17. {
  18. function __construct()
  19. {
  20. echo "this is child class <br>";
  21. parent::full_function();
  22. }
  23. // completing the abstruct function
  24. public function abs_function()
  25. {
  26. echo "this is completed abstract function <br>";
  27. }
  28. }
  29. $obj = new SubClass();
  30. $obj->abs_function();
  31. ?>

NOTE:

  • We can not call abstract class. An abstract class can only be extended.
  • Sub class can still use its properties.
  • An abstract class is used to restrict sub class and for separating interface from implementation.

Interface:

Interface is a collection of abstract functions. A class can implement more than one interface. Class, which is implementing any interface, must implement all functions of the interface. Complete image of interface is:

  1. <?php
  2. // defining an interface
  3. interface MyInterface
  4. {
  5. // abstract functions
  6. // all must be public
  7. public function display();
  8. public function another($argument);
  9. }
  10. // another interface
  11. interface AnotherInterface
  12. {
  13. public function complete_it();
  14. public function one_more();
  15. }
  16. class Parent
  17. {
  18. function parent_fun()
  19. {
  20. echo "parent function";
  21. }
  22. }
  23. // class extending parent and implementing both interfaces
  24. class Demo extends Parent implements MyInterface, AnotherInterface
  25. {
  26. public function display()
  27. {
  28. echo "display complete";
  29. }
  30. public function another($argument)
  31. {
  32. #code
  33. }
  34. public function complete_it()
  35. {
  36. #code
  37. }
  38. public function one_more()
  39. {
  40. #code
  41. }
  42. }
  43. ?>

NOTE:

  • All function inside the interface must be public.
  • A class can extend another class and also implement some interface. Class must declare parent class before interfaces.


About Harish Kumar

Harish is an interested person in the field of web development and blogging. He works for the need of young web developers in learning various languages, latest technologies and other essential tips and tricks. If you need some help or you have some suggestion then you email him at harish@findalltogether.com without any hesitation. You can also suggest/demand for articles of your own choice.

Related Articles

In this part of free tutorial series, I am telling you about syntax, variables, data types, comments, constants and o...
Introduction to PHP
In this part of free step by step tutorial series I will teach you about for loop, while loop and do while loop of PH...
loops in PHP
In this part of free step by step tutorial series I will teach you about if, if else, nested and switch statements of...
Conditional statements in php

Login or Sign up to leave comment.