Hello,
I was just looking for some advice on when to use a class in PHP instead of a function. Are they simply better to use than a normal function?
For example, if I wanted to connect to and select a database would I be better to use a class or function?
Thanks for your help,
Lewis
Using PHP OOP
Moderator: General Moderators
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Using PHP OOP
Personally speaking if you can get away with using just a function, use just a function. Otherwise its good to use a class, if you are going to use the same functions (methods inside a class) again and again. You could use a class and inside the class a method that connects to the database.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Re: Using PHP OOP
Use a class only when you understand how using a class helps you. Otherwise you gain nothing, you might as well use a function.
Re: Using PHP OOP
thankyou for your replies
lewis
lewis
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Using PHP OOP
Use a function only when you understand how using a function helps you. Otherwise you gain nothing, you might as well use a class.
(#10850)
Re: Using PHP OOP
OOP is something that you will begin to understand the benefits of only after programming for a while. I'd just use what you are comfortable with until you are no longer comfortable with it. Good luck!