r/PHPhelp • u/mprz • Oct 28 '14
Critique my code please
Hi. Tried some php coding last year and ditched it in favour of python, but want to get back to it. Could anyone have a look and let me know how bad/good is it? Many thanks.
6
u/winzippy Oct 28 '14
For someone that was just 'trying' PHP, I think you did pretty well. That said, here's my take. It's an incomplete list, but hopefully it's enough to be useful.
Avoid These
Don't interleave your logic with your output. The larger your app gets, the harder it will be to debug and manage.
Use autoloading instead of includes. As a maintainer of a nasty legacy PHP app, I can attest to the negative affects of kitchen-sinking it.
Use a templating engine, or roll your own. Stay away from simply echoing HTML in a function.
Stay away from the sexy two-line if statements. They're arguably cooler, but can be easily fudged or misread, producing logic errors. This is more of a nitpick than an actual issue.
Stay away from the Singleton pattern (Your DB class uses this). They're hard to debug. Instead, use a configuration object and create your DB connection as needed.
I wouldn't bother sticking an index.php in your css or js folders. There are cases where you may need to hide a potential directory listing, but in your case probably not.
Check out the MVC pattern. You're almost doing it here.
Continue These
Using PDO
Good formatting. Your code was very easy to read.
Catching and handling your errors! I don't see this enough.
For further reading:
PHP Objects, Patterns, and Practice ISBN: 143022925X
That's all for now. I hope you found this helpful. I Started writing this at about 6am (before coffee), so take it with a grain of salt.
1
Oct 28 '14
https://github.com/mprz/DomainAgent/blob/master/includes/classes/PasswordHash.php
... no. The person who wrote this wasn't forward thinking and couldn't even be bothered to implement a time constant compare function.
use password_hash() and password_verify(). If these are not available, use this fallback solution.
0
Oct 28 '14
[deleted]
2
u/mprz Oct 28 '14
Thanks for the input. I'm in between using Twig&Silex and full fledged framework such as Laravel.
1
u/scriptmonkey420 Oct 28 '14
Personally I stay away from most frameworks, they hide the underlying language so that you are really programming in their special syntax and not the language you chose. But that's the way I like to program.
3
u/aisas Oct 28 '14
Use some templating engine (e.g. Twig), so you html and php won't mix. Read PSR-2.