If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Why we need abstract classes and interfaces in php?

Started by Chiru, Mar 21, 2023, 06:44 AM

Previous topic - Next topic

ChiruTopic starter

I have come across numerous questions and articles discussing the significance of abstract classes and interfaces. However, I find myself repeating a question that has been asked thousands, if not hundreds of thousands of times before me: Why do we need abstract classes and interfaces, especially in PHP?

From my understanding, an abstract class serves the purpose of declaring methods for child classes, without necessarily defining all the parent methods in the descendant class. This feature sets it apart from regular classes, as it contains "abstract" in the name of some methods and the class itself cannot be instantiated. Now, my question is: Why do we need abstract classes if they seem to serve a purely decorative purpose and restrict the developer's actions? Is it merely a matter of convenience?

Similarly, I wonder about the necessity of interfaces. If their methods and properties are ultimately redefined, what is the advantage of using interfaces over inheriting from a regular parent class where properties and methods can be defined without implementing them? Traits exist as well, and their role seems relatively straightforward.

Traits are employed to avoid the need for creating parent classes and inheriting from them repeatedly, thereby preventing the creation of complex inheritance chains. By defining a method in the parent class and then connecting the trait to the inheritor class, the parent method can be overridden easily, eliminating unnecessary complications.

Given the availability of traits, I am left wondering: what is the purpose of abstract classes and interfaces?
  •  


cristine410

If we want to ensure clear understanding with a simple example, let's say we are creating a class for interacting with a database. This class should be able to work with different types of databases such as MySQL and PostgreSQL. To achieve this, we implement each database type as a separate class. By doing so, we can easily add new database classes to our project without having to rewrite the main class.

This approach allows us to create other classes based on our abstract main class, without worrying about missing methods (as abstract methods must be redefined when extending the functionality of the base abstract class). This prevents situations where a developer forgets to redefine a necessary method for the extension, or when other programmers neglect to do so when creating their own extensions. In such cases, the compiler will notify them that they need to redefine these abstract methods, as they are essential to the extension's functionality.

I believe this explanation provides some clarity. This concept is extremely useful, making it one of the most convenient and necessary features in PHP and other programming languages.
  •  

pauldave

An interface serves as a contract for a class, allowing users to understand how to utilize the class. It provides an alternative to multiple inheritance. Consider two types of collections: binary tree and graph. These data structures are quite distinct, but both can implement the Iterator interface. By doing so, the interpreter will know how to iterate through the collection in a foreach loop.

Abstract classes are primarily used when a portion of the code can be defined in the parent class, but requires specific details in order to be meaningful. These details are added through methods or fields.
If we observe abstract classes in modern frameworks, we'll notice that the abstract class itself may not have much significance. For instance, if you create an object of such a class using the new operator, it may still lack certain elements, which are then provided by the child classes.
  •  

alex.thomson

The interface is defined by the base abstract class. Subsequently, various child classes provide different implementations for different types of objects. However, with the help of the base abstract class, it becomes possible to create these diverse objects using a single factory or utilize them in other design patterns. The classes in these patterns can interact with these objects through the interface without needing to know the specifics of their implementation.

Taking the first step, it is important not to overlook the second one. It is valuable to delve into topics like SOLID and GRASP for a better understanding of design patterns. As you enhance your abstract thinking, you will gain a deeper comprehension of Object-Oriented Programming (OOP) beyond the simple mantra of encapsulation, inheritance, and polymorphism. In large commercial projects, abstractions play a significant role. There, everything tends to be highly abstract.
  •  

anilkh7058

  •  

Emaidavom

Abstract classes and interfaces serve different purposes in PHP, and their usage depends on the specific scenario and design requirements.

Abstract classes allow you to define common behavior and attributes for a group of related classes. They provide a way to partially implement a class by declaring abstract methods that must be implemented by any concrete (i.e., non-abstract) subclass. Abstract classes can also provide default implementations for certain methods, allowing subclasses to override them if needed.

The primary benefit of abstract classes is to establish a common interface or contract that subclasses must adhere to while providing some shared functionality. This promotes code reusability and helps enforce consistency across related classes.

Interfaces, on the other hand, define a contract that a class must follow without dictating any specific implementation details. An interface is essentially a list of method signatures that a class must implement. By implementing multiple interfaces, a class can provide different sets of behavior without being bound to a specific class hierarchy.

Interfaces are useful when you want to define a set of methods that multiple unrelated classes should implement. This promotes loose coupling between components and allows for more flexible code design. It enables you to interact with objects based on their shared behavior, rather than their specific class hierarchy.

While traits offer reusability of method implementations, they cannot serve as a contract or define a common interface like abstract classes and interfaces. Traits are best suited for sharing reusable code across different classes, avoiding the limitations of single inheritance.

Overall, abstract classes and interfaces provide ways to structure your code, promote code reusability, enforce contracts, and enable loose coupling between components. Each has its own use case, and understanding when to use them depends on the specific design needs of your project.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...