Back to Top

PHP 5 Abstract classes and Methods

PHP 5 Abstract classes and Methods

Simply say Abstract method means We declare it in one class and use(define) it in other class.

What is Abstract in PHP?

PHP 5 supports the abstract classes and methods in OOPs concept.A class contain the abstract methods is known as an abstract class and that class specified with the keyword abstract.An abstract class can not instantiate objects means if you are trying to instantiate class with a new keyword which is not possible and class must be specified with at least one abstract method and When you inherit from an abstract class, all the abstract methods in the superclass must be defined in the sub(extended) class.

Note that an abstract class contain both the abstract methods and other methods.

ABSTRACT Method FORMAT:

Introduction:

  • Abstract class was introduced in PHP 5.3 and in OOP, it’s not in PHP 4
  • Prefix or Keyword: “abstract” for class and method
  • Concept: we need to extend this class by its Superclass or classes.

The concept of the abstract class came from Java.Abstract class mainly create confusion for us because of Interface.The abstract class can’t be directly initiated with the new operator, we need to extend it with other class which is not abstract.

Recommended Read: Constructor and Destructor in PHP and Type Hinting in PHP5.

An abstract class isn’t a fully defined method because we need to create other class to extend it.

NOTE We can derive class from abstract class. Object of an abstract class can’t be possible.

Basic difference between Interface and Abstract class:

The interface can be implemented and Abstract class can be extended and PHP can support multiple interfaces, but it does not support multiple inheritances with class extend.It only supports multi-level inheritance so I can say if multi-level inheritance required or multiple inheritances is not required, you can prefer Abstract class.

Access modifiers(Public, Protected and Private) are not mandatory but if you are using it,make sure the method declared in subclass have same access modifier or less when creating a subclass of extending abstract class.

Let’s see example how access modifier works with Abstract

Here, In above example have protected access modifier for abstract method showarea(), so in the subclass you need to use protected or public access modifier. If you try to use private, it will generate fatal error
Fatal error: Access level to Triangle::showarea() must be protected (as in class Shape) or weaker

Basic Syntax to declare Abstract Class and Abstract Method:

NOTE: If any class contains abstract method then the class must be declared as abstract.

Important Points:

  1. In Abstract class, we can declare abstract method but we can’t define it.
  2. The Subclasses which inherits the property of abstract class, must define all the methods declared as abstract.
  3. If a method is defined as abstract then it cannot be declared as private (it can only be public or protected) otherwise its can not be inherited.
  4. Method declared on the subclass must have a same or lower access level that the abstract method declared in the super class. So if super class have protected abstract method then we can have that method either public or protected but we can not declare it as a private.

Let’s move to one Example so you get straight-knowledge of it.

Let’s create one PHP application which declare abstract class Shape with two members say d1 and d2.This class declare abstract method show area which inherit the class triangle from Shape class.You can create a class with the keyword abstract even if it does not have a single abstract method.

We can say Shape is our Superclass and Triangle is the subclass. Subclass Triangle defines Showarea method declare as abstract in shape class.We create one object of Triangle class and will display it and make it sure that Object of abstract Shape class can’t be created.

Here, New operator or I can say Object used to call Triangle class which extend our Abstract class and Triangle class has one method showarea() so it will call abstract class Shape because method is declared in abstract class so indirectly new operator will call the Shape Class.

NOTE: New operator indirectly call the super class or Abstract class by using a subclass.

If you need help for developing code using abstract, feel free to ask me via the comment section below.

Comments (2)

  1. […] inheritance which is not supported with abstract.In one of the earlier post,I have explained about Abstract in […]

  2. […] inheritance which is not supported with abstract.In one of the earlier post,I have explained about Abstract in […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to use HTML5 ContentEditable attribute

Posted on 3 years ago

Bhumi

Learn about Variables in LESS CSS

Posted on 3 years ago

Bhumi

A Beginner’s Guide To Composer

Posted on 1 year ago

Bhumi