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

 

Transitioning to OOP: Benefits and Considerations for Web Projects

Started by Deepak1, May 27, 2024, 12:25 AM

Previous topic - Next topic

Deepak1Topic starter

Good day.
For the past 6 years, I've been creating web applications, mostly for small businesses, working independently without subcontractors.
Here's the breakdown: PHP (sometimes NodeJS) + MySQL (occasionally Postgres and SQLite) + frontend (I won't go into detail).



The technology operates as follows:

    The client (browser) accesses the necessary RPC API procedure (via XMLHttpRequest), while sending an authorization token.
    The procedure processes the incoming parameters, accesses the Database (via the PDO class) if needed, and sends a JSON response back to the client.

I can reuse procedures across projects instead of rewriting them every time.

I recently came across an article that claimed if you don't code in OOP style, you're not doing it right. It got me intrigued - why is that?

With OOP, my APIs wouldn't directly access the database but would interact with instances of object classes.
Can anyone explain the specific advantages of this shift? When should I consider transitioning to OOP, based on project size?
  •  


Buioze

Let's delve into the potential advantages of transitioning to OOP from the standpoint of a web developer. Object-oriented programming offers a structured approach to code organization, emphasizing modularity, reusability, and maintainability. With OOP, you can encapsulate data and behavior within objects, promoting a clearer delineation of responsibilities and relationships within your codebase. This can lead to reduced code duplication, improved scalability, and enhanced readability.

In the context of your web applications, adopting OOP would entail refactoring your existing procedural API procedures into object-oriented classes. By doing so, you can encapsulate the logic for interacting with the database within these classes, which fosters a more modular and flexible architecture. OOP enables the use of inheritance and polymorphism, allowing you to create a hierarchy of classes that share common functionality, thus reducing redundancy and facilitating easier code maintenance.

Notably, the transition to OOP can provide alignment with industry best practices and modern web development standards. Many popular web frameworks and libraries are built around OOP principles, making it simpler to integrate your applications with these tools. Additionally, as the scope and complexity of your web projects grow, OOP can help maintain the overall code quality, making it easier to introduce new features and enhance existing functionality.

When considering the right time to migrate to OOP, it's essential to assess the size and complexity of your projects. For smaller, more straightforward applications, the immediate benefits of OOP may not be as pronounced. However, as your projects expand, the modular, extensible nature of OOP can provide significant advantages in terms of codebase maintenance, feature enhancements, and team collaboration.
  •  

mbuilders

Object-oriented programming (OOP) streamlines the creation of programs with dynamic components. It's not a must for success in web design; a web designer can thrive without OOP knowledge. However, OOP offers valuable benefits.

1) Encapsulation - It involves concealing the internal state and enabling controlled modification through specific "controls."
- Extensive work with communication protocols (e.g., email).
- Managing entities with variable states (limited in web design - perhaps automatic layout management?)

2) Abstraction and polymorphism - the ability to support diverse entities under a unified interface.
- Adapting to different technologies - for instance, using an abstract class "DB" and deriving MySQL and SQLite from it.
- Application in specific industries - such as structuring characters in a game. Although creating a multiplayer game entirely in PHP might be challenging.
- Other potential applications? Is there a need for a desktop/mobile version?
  •  

Affemabib

No compelling arguments have been presented in support of OOP in PHP. It all boils down to 'hey, give it a try and you'll see how awesome it is.'

When developing a GUI application that runs continuously until the user stops it, OOP is indeed recommended, especially in the interface programming stage. However, in the case of PHP, the program operates for a fraction of a second (while handling the request) and doesn't require the same level of interface programming. The objectives of a PHP program are clarity and speed, which are not specifically met by OOP.

In essence, the program requires simple, intuitive logic (which is often overlooked), code devoid of redundancy, and documentation. It does not need additional syntax to enhance its speed.

Upon examining Laravel, I notice a few worthwhile components that could be better executed as functions, along with a surplus of code simply for the sake of it.

For instance:
$users = DB::table('users')->get();

Nevertheless, there is no prohibition against creating a logically polymorphic function q():

$users = q("users");

This function can handle an SQL query or a more sophisticated super-SQL construct, yet remains more comprehensible than a chain of methods. Some may argue that the function will be tied to a specific type of database. However, it's important to note that, depending on the database type used, there's nothing to prevent us from retrieving the necessary file containing the appropriate function implementation, such as mysql.db.php or postgresql.db.php.

If the logic of procedural code is well-balanced, free of code duplication, and well-documented, it will outperform OOP code in terms of accessibility and speed. Considering that OOP code also requires balance and documentation, procedural code's advantages become unequivocal.
  •  


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