r/PHPhelp • u/thegamer720x • Apr 08 '25
Right way to PHP OOP Implementation
Hi, I'm working as a full stack php developer. My job mainly requires procedural style php code. So I'm quite well versed with it.
Now, I have been trying to learn php oop. But no matter how many videos or guides i see, its still confusing.
Main confusion comes from
- File organization
- should each class be a seperate php file
- should utility class ( sanitization, uppercase, lowercase etc) be all combined in one?
- how to use one class in another class
- How to create class
- what should constitute a class. Should user be a class defining creation / deletion / modification of users
- what exactly should a constructor of class do ( practically)
I'm still trying to defer mvc architecture for now. In order to understand how the design and flow for oop program should be done
Any and all help with this is helpful. I understand the basics, but having difficulty with irl implementation. Please recommend any guide that helps with implementation rather than basics.
Thanks
View all comments
Show parent comment
2
u/equilni Apr 10 '25 edited Apr 12 '25
To start:
https://phptherightway.com/#common_directory_structure
https://github.com/php-pds/skeleton
https://www.nikolaposa.in.rs/blog/2017/01/16/on-structuring-php-projects/
I typically quote this:
You could mix PDS-Skeleton and Slim's config files. Which means, to start:
Begin pseudo code:
settings.php. This could be a simple returned array like the linked Slim exampledependencies.phpwould house all your class instances and allow for a Dependency Injection Container like PHP-DI. This could look like:routes.phpcan hold the route definitions. If you are using this setup, you cannot directly link to files like how some beginner procedural setups are. You would need to send the requests to thepublic/index.phpthen come here torouteagainst./public/index.phpThis is the only public PHP file (a server config). This can start the application, get the request, send it inward, then receive the response back. OR just call another file internally that does this - typically another bootstrap file.In this example, I call the relevant files, then process the route request. This is almost identical to how the Slim Framework has it's skeleton application:
/public/index.php
Run the routes and emit the response.
https://symfony.com/doc/current/introduction/http_fundamentals.html
to send domain-layer results to your user-interface layer, along with meta-data indicating the meaning of the domain results..https://github.com/auraphp/Aura.Payload/blob/HEAD/docs/index.md#example
More on ADR (like MVC), same author as the MLAP book and above library (this uses a different version of that library) - https://github.com/pmjones/adr-example
https://github.com/slimphp/Tutorial-First-Application
Write up on this and adding ADR
https://www.slimframework.com/docs/v3/tutorial/first-app.html
https://www.slimframework.com/docs/v3/cookbook/action-domain-responder.html
That's a little difficult, most projects (not libraries) are done via a version of MVC.
MVC can be done procedurally too.
pseudo code to illustrate an idea.
Now change this to classes/objects: