Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functions vs OOP question

Tags:

php

I have just started studying OOP and I am a little confused of the benefits of doing either functions or OOP. I understand OOP is much more versatile in organizing your code, but for example, I have 50 lines of code, that occur on 7/10 pages of my site. Is it better to create a file and put a function in the file, and then use an include, or do the same with a class? Furthermore, is it better, to mass classes into one file and include it? If anyone has a bit of insight that may help clear up my confusion, I would really appreciate it.

like image 619
Scarface Avatar asked Mar 17 '26 21:03

Scarface


1 Answers

The appropriate metric is usefulness.

If this code does just one thing, accepts a few input parameters, and returns a single result, then it's a function.

If the code can collect data, and does something tricky on this data, and you can specifically reuse results or get different results, then turn it into an object.

like image 52
mario Avatar answered Mar 20 '26 11:03

mario



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!