r/PHPhelp • • Feb 01 '18

Am I doing OOP the wrong way ?

Ok, so I've been coding for a few years now, but I have a bias. I code mostly alone, delivering packaged scripts to customers, whether in vanilla PHP/MySQL (+some kind of a front but not always) or as a Prestashop/OpenCart/WordPress plugin.

I recently coded a scraping script : getting listings + ads every day, geo matching them and mailing them to the relevant targets, and I used the opportunity to upgrade my "not a framework more a set of functions organized in objects, like a HTTP object and a Text Object and a bare controller", and while browsing some tutorials and references, I once again asked myself if i was doing OOP The Right Way (spoiler : I'm not, but I'm willing to improve).

Brief view of the index.php code to have an actual example :

$Main = new MainSortOfAController(); // on construct : parse query, set up $Main->db = new DB() and $Main->http = new HTTP()
$Listings = new SourceListingsClass(); // class SourceListingClass (specific to the source of data) extends ListingClass
$Ads = new SourceAdsClass(); // idem
// sample action
$Listings->fetch_listings();

The Listings Class has methods like fetch_listings(), parse_listings(), etc.

Classes usually stores non writable data in $this->data (with setData($id,$value) - conditional on $this->data[$id] being not set before, and getData()) and stores loop data (rewritten at each iteration) in $this->current (with setCurrent() and getCurrent, etc.)

In other words, I might be using objects as (very fat) controllers everytime and not as proper objects. I don't have any Listing class, or Ad class, but Ads class that fetch, parse and store data in loops.

I am interested in feedback, since I need to improve the maintainability and readability of my code, and am interested in changing the way I code, and maybe follow a mini framework project.

Performance is actually pretty good, all SQL statements are prepared in the DB class, HTML is in the /templates/ directory (ie be gentle with me)

Sidenote : plenty of websites/tutorials/books to explain the "how ?", very few if any to explain the "why ?". Design choices, script architecture, etc.

7 Upvotes

4 comments sorted by

1

u/[deleted] Feb 01 '18

There are resources out there that explain the "why" and go into architectural/design choices. Try picking up a copy of a book like "Head First Design Patterns". "Patterns of Enterprise Application Architecture" is another well regarded one, but to be honest it's about as interesting to read as the title makes it sound.

Googling "design patterns" and reading around the results there should help you out too. From what you've written, I think something like the Repository patten could be a good start.

1

u/Malaiac Feb 01 '18 edited Feb 01 '18

That's a common problem : either the books/ressources are waaaay to abstract (and seem to be written by someone who wants to show off how complex he can design), or waay to simple/irrealistic (like the classic Persona/Magician/Warrior example)
Thanks for the feedback

1

u/[deleted] Feb 01 '18

Another way you could approach the problem is to write a really good unit test suite for your code. This makes the code more maintainable, because you have the test suite can can detect breakages instantly. It'll also force you to think about the architecture more, as a codebase that's better written is usually easier to write tests for. (Because of things like the coupling between components being low, and fewer responsibilities per component, etc.) I've got "The Grumpy Programmer's PHPUnit Cookbook" which is a really good intro to that sort of thing.

1

u/cyrusol Feb 04 '18 edited Feb 04 '18

They only seem way too abstract shortly after trying them out. Later on you will realize for yourself why these people designed some of those classes the way they did.

Part of the "problem" (if you wanna call it that way) you have is that you work alone. Trust me, if you had to work with someone else's code it is far easier to get mad when reading those 2500 lines long do-almost-everything functions cluttered throughout a large codebase. Also nobody has to read (and therefore nobody will get mad at) your code.

The longer you work with the same code/project you will see that it is actually simpler to have many (thousands of) classes of which each fulfills exactly one purpose rather than having a few (hundreds of) classes of which each fulfill one or a few purposes.

A saddening example would be PHP's iterator interface. 5 methods you have to implement. But:

  • rewind is completely useless. Instead one should just create another iterator with their own, isolated state and not reuse existing ones.

  • current is useless because

  • next could just return the current element

  • validis not an iterators job to care about! Because next could just return an Option type that is either the current element or something representing null. Separation of concerns makes interfaces/APIs much easier to use.

  • key is just wrong (too specific). Not every iterator needs to iterate over something that has a key. (If it had a key current could just return a tuple (array or object with a key and a value property) - or considering the point I made for valid: such a tuple wrapped in an Option.

If the PHP language designers would have considered these design aspects we could have had a sane iterator interface where you just had to implement:

interface Iterator {
    public function next(): Option;
}

Here we have it. A much simpler interface, you only have to do the thing you actually care about when you want to abstract the looping over something. Just by applying sane principles.