Advantages and Disadvantages of Inheritance in php

0 votes
Could someone please explain the main benefits and drawbacks of inheritance and multi-level inheritance in PHP? In advance, many thanks.
Jul 28, 2022 in PHP by Kithuzzz
• 38,000 points
721 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Advantages:-

By distributing common code among numerous subclasses, inheritance helps applications contain fewer duplicate lines of code. When the same code appears in two classes that are connected to one another, the hierarchy can typically be refactored to promote the shared code to a mutual superclass. Additionally, this usually leads to smaller, simpler compilation units and better code structure.

Because classes that inherit from the same superclass can be used interchangeably, inheritance can also make application code more adaptable to changes. Whenever a method's return type is the superclass.

Reusability: The ability to use base class public methods without having to recreate them Extensibility: Extending base class logic to accommodate derived class business logic Data hiding: The base class can choose to keep some data private so that it cannot be changed by the derived class

Overriding—Thanks to inheritance, we are able to construct useful implementations of base class methods in derived classes by substituting our own methods for those of the base class.

Disadvantages:-

  1. The extra time and effort required for the programme to navigate through all the levels of overloaded classes is one of the key drawbacks of inheritance. It will essentially require ten hops to go through a function defined in each of the classes that are ten levels of abstraction above a specific class.
  2. The main drawback of inheritance is the close coupling between the base and inherited classes. This implies that one cannot be used without the other.
  3. Additionally, over time, it becomes necessary to modify both base and derived classes when doing maintenance or adding new features. We will be impacted in both scenarios if a method signature changes (inheritance & composition)
  4. We will need to re-factor if a method in the "super class" or aggregate is deleted and we need to use that method. In this scenario of inheritance, things can become a little more tricky because while our programmes will still compile, their methods will no longer override those of the superclass. These techniques will develop into stand-alone techniques.
answered Jul 29, 2022 by narikkadan
• 63,600 points

edited Mar 5

Related Questions In PHP

0 votes
1 answer

How to validate E-mail and URL of Php form?

hey, The code below shows a simple way ...READ MORE

answered Feb 13, 2020 in PHP by manish
2,499 views
0 votes
1 answer

What is meant by passing the variable by value and reference in PHP?

Hello, When the variable is passed as value ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,840 points
3,421 views
0 votes
1 answer

What is type casting and type juggling in php?

Hey, The way by which PHP can assign ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,840 points
7,316 views
0 votes
1 answer

What is the use of $_REQUEST variable in php?

Hii @kartik, The $_REQUEST variable is used to read the ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,840 points
3,640 views
0 votes
1 answer

How to merge two arrays while keeping keys instead of reindexing in php?

Hello, Considering that you have $replaced = array('1' => ...READ MORE

answered Apr 1, 2020 in PHP by Niroj
• 82,840 points
3,185 views
0 votes
1 answer

How can I handle the warning of file_get_contents() function in PHP?

Hello @kartik, This is fairly simple: if (!$data = ...READ MORE

answered Apr 7, 2020 in PHP by Niroj
• 82,840 points
11,466 views
0 votes
1 answer
0 votes
1 answer

C++ - Overloading vs Overriding in Inheritance

In C++, a derived class's method only overrides the base class's method if their declarations match (I say "match," but I'm not sure what the formal term is).  That is, all arguments must be of the same type, with the same const qualification.  If there are any mismatches, the derived class's method hides all methods with the same name rather than overriding.  This is what the "ERROR" in your image is attempting to convey.  So, in that image, / overrides in a comment is incorrect and misleading. Yes, many C++ instructors are unaware of these somewhat esoteric details. Furthermore, if you want to override, your base class's method must be virtual; otherwise, polymorphism will not work. . We could also say that the derived-class method hides the base-class method if it wasn't virtual.  The part about hiding, on the other hand, has almost no meaning here; what this term really means is that you're not in charge. Furthermore, overloading is the presence of multiple methods with the same name but different signatures, as you may have noticed. To be useful, they must all be present in the derived class; otherwise, they will be hidden if the derived class only has one method, fa1, and the other fa1 are in the base. There is, however, a syntax sugar that "copies" all fa1 from base to derived, removing all the hidden semantics: class A { public: void fa1(); ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
909 views
0 votes
1 answer

C++ - Overloading vs Overriding in Inheritance

In C++, a derived class's method only overrides the base class's method if their declarations match (I say "match," but I'm not sure what the formal term is).  That is, all arguments must be of the same type, with the same const qualification.  If there are any mismatches, the derived class's method hides all methods with the same name rather than overriding.  This is what the "ERROR" in your image is attempting to convey.  So, in that image, / overrides in a comment is incorrect and misleading. Yes, many C++ instructors are unaware of these somewhat esoteric details. Furthermore, if you want to override, your base class's method must be virtual; otherwise, polymorphism will not work.  We could also say that the derived-class method hides the base-class method if it wasn't virtual.  The part about hiding, on the other hand, has almost no meaning here; what this term really means is that you're not in charge. Furthermore, overloading is the presence of multiple methods with the same name but different signatures, as you may have noticed. To be useful, they must all be present in the derived class; otherwise, they will be hidden if the derived class only has one method, fa1, and the other fa1 are in the base.  There is, however, a syntax sugar that "copies" all fa1 from the base to the derived. class A { public: void fa1(); ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
1,292 views
0 votes
0 answers

Is it possible to write strictly typed PHP code?

Is it possible to write code like ...READ MORE

Jun 6, 2022 in PHP by Kichu
• 19,040 points
486 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP