Advantages and Disadvantages of Inheritance in php

28,951

Solution 1

Advantages:-

One of the key benefits of inheritance is to minimize the amount of duplicate code in an application by sharing common code amongst several subclasses. Where equivalent code exists in two related classes, the hierarchy can usually be refactored to move the common code up to a mutual superclass. This also tends to result in a better organization of code and smaller, simpler compilation units.

Inheritance can also make application code more flexible to change because classes that inherit from a common superclass can be used interchangeably. If the return type of a method is superclass

Reusability -- facility to use public methods of base class without rewriting the same Extensibility -- extending the base class logic as per business logic of the derived class Data hiding -- base class can decide to keep some data private so that it cannot be altered by the derived class

Overriding--With inheritance, we will be able to override the methods of the base class so that meaningful implementation of the base class method can be designed in the derived class.

Disadvantages:-

  1. One of the main disadvantages of inheritance is the increased time/effort it takes the program to jump through all the levels of overloaded classes. If a given class has ten levels of abstraction above it, then it will essentially take ten jumps to run through a function defined in each of those classes

  2. Main disadvantage of using inheritance is that the two classes (base and inherited class) get tightly coupled. This means one cannot be used independent of each other.

  3. Also with time, during maintenance adding new features both base as well as derived classes are required to be changed. If a method signature is changed then we will be affected in both cases (inheritance & composition)

  4. If a method is deleted in the "super class" or aggregate, then we will have to re-factor in case of using that method.Here things can get a bit complicated in case of inheritance because our programs will still compile, but the methods of the subclass will no longer be overriding superclass methods. These methods will become independent methods in their own right.

Solution 2

Not sure if this is the right site for this type of questions, but okay.

Let's say you have some basic class Base, which every other class extends. In base you can define things that all other classes can use, e.g. connection to mysqli, so all classes can use $this->mysqli and don't need to have it declared.

I built an ecommerce cms, where I use it this way. I also defined a method called getLink() which checks if there's a method getUrl() and getTitle(). These methods are defined in every child class (at my CMS Product, Category...). Let's take the Product class, there are also the methods getUrl() and getTitle() but the method getLink() is in the Base which the Product extends. I use the method getLink() almost everywhere so if I don't want to write it in every class, I just write it in the Base and I can use it.

Another example from my CMS: in the Base construct I have a check if input with name global_update was submitted. It will check if $this->table and $this->id was defined and if yes, it will update database with the data. Otherwise I would need to define a handler for every update action. This way I have one global for every class and I write own handlers only for more complex forms.

I hope this is understandable, if not, comment and ask.

Share:
28,951
Priyank
Author by

Priyank

I am a senior software engineer developing a web and mobile application. My Skill set is Angular, Ionic, PhoneGap, Ios, Worklight (MobileFirst), Xamarine, PHP, Jquery/Javascript, Mysql, MongoDB... keep learning :)

Updated on July 07, 2022

Comments

  • Priyank
    Priyank almost 2 years

    Can anyone tell me what are the Main Advantages and Disadvantages of Inheritance/multi-level inheritence in php.Thanks in Advance :)

    • Utkarsh Dixit
      Utkarsh Dixit over 9 years
    • Priyank
      Priyank over 9 years
      what is the reason to downvote my answer?
    • Utkarsh Dixit
      Utkarsh Dixit over 9 years
      For your answer or question. ( I haven't downvoted your answer)
    • Priyank
      Priyank over 9 years
      @user3647254 i am not pointing you but someone downvote my answer without giving any reason :( (like what is wrong in my question)
    • Utkarsh Dixit
      Utkarsh Dixit over 9 years
      You haven't answered anything yet, how can someone downvote