Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 07/06/2023 in all areas

  1. An object-oriented approach has a lot of benefits over a functional approach, particularly when paired with a good IDE. Autoload support If you structure your classes according to the PSR-4 standard, you can let an autoloader script manage including your classes when needed. This means you no longer have to manual deal with making sure you have included the correct files on your pages, or just include everything to be safe. You can just use your classes as needed and let your autoloader script automatically include the class file when necessary. Autoloading of functions is currently not possible, though that might change eventually. Data management Something every program needs to do is manage and work on data. With a functional approach, that often means you use a lot of associative arrays to hold your data and pass those array's between functions. While this works, over time it becomes hard to manage as you will loose track of what keys exist in what arrays and different code paths may end up diverging in how they manage those arrays. Even using just simple objects can help with this by providing predefined properties for your data structures. When paired with a good IDE, you will be able to able to easily see what properties are available on an object, get auto-completion of property names as you code, and easily refactor the data structure if necessary (ie, rename a property). Automated Testing OOP lends itself well to automated testing thanks to features like interfaces and inheritance. With functions, you end up with hard-coded references to other functions through out your code. The implementations of those functions cannot be easily changed for testing. By passing around objects and calling methods instead, you can easily replace your real object with a fake / mock object for testing purposes. To take full advantage of this, you need to not just use OOP, but also techniques like dependency injection so you don't just replace a hard-coded function with a hard-coded class reference. Reuse While both functions and classes allow one to re-use code, I generally find that making reusable code is easier done using an OOP approach. Often in order to be reusable, code needs to be flexible. To manage that with functions you either end up making a lot of different functions, or your functions need to have a lot of parameters (or a single large array parameter) to control the functions behavior. This leads to a messy code base and can make future edits hard to make without inadvertently affecting some use case. With an OOP approach, you can control behavior though either properties that can be set individually as needed, or by breaking things down into various objects that can handle different use cases. This lets the code be cleaner by removing various conditional branches and helps isolate future changes to specific areas without affecting other areas. You need to know OOP anyway. The vast majority of libraries out there are using OOP, so if you want to take advantage of other peoples work to save yourself time (ie, the various frameworks or even just individual libraries) you need at least a basic understanding of how OOP works. You don't need deep knowlege of everything OOP and all the design philosophies t hat go with it just to use a few libraries, but you do need basic syntax and structure knowledge to understand the library code and know how to interface with it. The more you know though, the easier it is to integrate with libraries and customize them to suit your needs.
    1 point
  2. Welcome. You'll be wanting to look at prepared statements then. Take the PDO route and not mysqli - it's much better.
    1 point
This leaderboard is set to New York/GMT-05:00
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.