DEV Community

presh900
presh900

Posted on

2 1

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.

Neon image

Build better on Postgres with AI-Assisted Development Practices

Compare top AI coding tools like Cursor and Windsurf with Neon's database integration. Generate synthetic data and manage databases with natural language.

Read more →

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!!

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay