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
  8. ";
  9. }
  10. // abstract function
  11. abstract public function abs_function();
  12. function full_function()
  13. {
  14. echo "this is not an abstract function
  15. ";
  16. }
  17. }
  18. class SubClass extends AbsClass
  19. {
  20. function __construct()
  21. {
  22. echo "this is child class
  23. ";
  24. parent::full_function();
  25. }
  26. // completing the abstruct function
  27. public function abs_function()
  28. {
  29. echo "this is completed abstract function
  30. ";
  31. }
  32. }
  33. $obj = new SubClass();
  34. $obj->abs_function();
  35. ?>

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.


ReactJS with Redux Online Training by Edureka

About Harish Kumar

Harish, a fullstack developer at www.lyflink.com with five year experience in full stack web and mobile development, spends most of his time on coding, reading, analysing and curiously following businesses environments. He is a non-graduate alumni from IIT Roorkee, Computer Science and frequently writes on both technical and non-technical topics.

Related Articles

PHP stands for PHP: Hypertext Preprocessor. PHP is known as a server side scripting and interpreter language for web ...
Introduction to PHP
In this tutorial, we will study about conditional statements (if else statements and switch statements). These statem...
Conditional statements in php
In this tutorial, we will study about strings and operations upon strings. Due to long length of this section, I have...
Strings in PHP (part-1)

Complete Python Bootcamp: Go from zero to hero in Python 3

Top Posts

Recent Posts

The Complete Web Developer Course - Build 25 Websites

Subscribe

Subscribe now and get weekly updates directly in your inbox!