r/PHPhelp • • 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?

10 Upvotes

16 comments sorted by

2

u/Atulin Mar 21 '23

1

u/_ncko Mar 21 '23

That is PHP 8

2

u/CyberJack77 Mar 22 '23

Correct. If you don't use PHP 8 (8.0, 8.1 or 8.2) right now, you use an unsupported version and should update as soon as possible.

See https://www.php.net/supported-versions.php

This even shows that PHP 8.0 only gets security fixes, so it is recommended to use PHP 8.1 or 8.2 right now.

0

u/_ncko Mar 22 '23

Yeah I think that is the plan sometime in the next several years.

3

u/CyberJack77 Mar 22 '23 edited Mar 22 '23

No offense, but if it is that bad, why do you still work there? For what I've read, the software you maintain is very outdated, doesn't follow any standaards, and runs on a non supposed PHP version, so chances are the software is vulnerable (or worse). From experience I can tell that if these things aren't addressed soon, they probably never will.

You seem smart enough and want to do better, and that is great. But to me this feels like fighting an already lost battle.

I don't know where you are from, but where I live, companies are still looking for PHP developer's. Basic understanding of PHP and the will to learn can get you a long way, and good companies know that.

To help you now, there are some good links in the "Learning" section on the right side of this sub that can be help you.

5

u/_ncko Mar 22 '23

Because the point as an engineer is to improve things and some problems are big enough that they take a long time to fix. If there are no problems to solve, there is no job.

3

u/CyberJack77 Mar 22 '23

A valid point, just as long the rest of the company is willing to move forward as well.

2

u/BenL90 Mar 22 '23 edited Mar 22 '23

you can start iterate the fix one by one, and deploy it on containerise env... it will become better after that. We able to transition from PHP 4, 5.4, to 7.0, then now PHP 8.1, after about 4 years of planning and execution, with butchering some of the working services that already available, and go with microservice arch... so in short, it's having 2 service work together, and change/migrate it one by one, as time pass.

I hope lucks for you!

We do still have some 5.4 on prod, but starting to move away to 8.2 on next release one by one... it's the beauty of PHP.. you can run different version with proxy(nginx+swoole/nginx+roadrunner)... and it will helps... a lot!

2

u/greg8872 Mar 23 '23

From experience I can tell that if these things aren't addressed soon, they probably never will.

Very true!

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

u/itemluminouswadison Mar 22 '23

Not php specific but head first design patterns is amazing

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

u/doodooz7 Mar 22 '23

Learn Java