DEV Community

presh900
presh900

Posted on

Why I switched to Object Oriented PHP

Anyone starting programming with PHP automatically starts with procedural PHP. It's like the defacto mode for writing programs because it takes everything step by step and is easier to teach newbies.
Having worked a little with Java and noticing how oop made everything a little abstracted so you don't have to get your hands dirty, I knew if I wanted to write quality code, I will have to eventually switch to Oop PHP.
Here are some things I noticed.

Cleaner Code:
This is like a no-brainer, I put all database queries in a class in a 'classes' folder and when ever I needed to perform any query all I had to do was
$query = new DB;
$insert=$query->insert($Table, $column,$value);

Notice how clean it is??

Helps in understanding Frameworks:
All modern frameworks are written using oop so switching to this makes it easier to understand any framework at all. In my code. I implemented a logon class that logs in users and redirects them to Dashboards depending on whether they are admin or users. I implemented it with a static class like this
Auth::isAdmin and Auth::is user so when I saw almost the exact syntax when works with laravel, I understood it immediately.

Reduces Lines of Code:

This is a no-brainer. No need to write long lines of code to check for conditions or perform arcane logic or write 5 database queries in one script. All you need is to call the particular class and pass it the values and it performs everything for you.

Better IDE usage:
If you are writing Procedural PHP, you are not really making full use of your IDEs tools like intellisense and etc because they are built to work well with oop code and offer better code structures and autocompletes.

🤔 There are other benefits, just try it out. I will suggest learning resources for oop in my next post.

Top comments (4)

Collapse
 
yusufcodes profile image
yusufcodes

Yep OOP is super handy and is my preferred coding style.

Collapse
 
presh900 profile image
presh900

Exactly.

Collapse
 
jaguar48 profile image
jaguar48

This is great. Nice you find your path.

Collapse
 
presh900 profile image
presh900

Thanks!!