r/PHPhelp • u/_ncko • Mar 21 '23
Resources on coding WELL in a PHP?
About a year ago I started a new job that uses a lot of PHP. I've been able to pick it up as necessary along the way well enough to meet the demands of the job. However, I'd like to start improving things about our codebase. Part of that is introducing better patterns to our codebase.
I'm looking for resources on good programming patterns in PHP. I find PHP's lack of strong typing to be very frustrating to deal with in our codebase. Type hinting but no generics. Arrays that represent both sequential data and key-value pairs. No enums.
The underlying data is messy and they're not using an ORM. Introducing an ORM at this point is not feasible so we work directly with SQL strings. So one common thing are methods with lots of branching to generate a SQL string.
They updated from PHP 5 to PHP 7.4 some time ago. But our dependencies are out of date. We are stuck with PHPUnit 5-something and we have no mocking library. Their testing is terrible. I'd love to find a way to update PHPUnit but it will probably break a lot of people's code.
It seems to me that being productive on a large codebase in PHP requires having strong conventions in place. Unfortunately, that ship sailed long before my time at this company.
Are their good PHP-specific resources on useful, reusable patterns? Or do I just need to adapt patterns from other languages to PHP?
3
u/BinBashBuddy Mar 22 '23
Incorporating an ORM is pretty simple and shouldn't break any existing code. I'm working with some really terrible legacy code, I replaced the mysqli and concatenated sql strings with PDO and stored procedures, but a couple years ago pulled illuminate into the codebase and now every time I hit sql I change it over to use illuminate models. Illuminate is the ORM that Laravel uses.
1
1
u/Electronic-Bug844 Apr 13 '23
I do both Typescript and PHP and while I do agree PHP is nowhere near the power of TS, there are some things you can do to make your IDE better understand. For instance, you can do PHP docbloc to typehint array of object parameters:
/*
param Foo[] $f */ public function bar($f) { ... }
I'm regards to principles, it's pretty much going to be standard stuff you've heard of like SOLID, DRY, KIS.
Though these are just principles and aren't always the best way to do things depending on your app.
-9
2
u/Atulin Mar 21 '23
https://www.php.net/manual/en/language.types.enumerations.php