Using PHP OOP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lshaw
Forum Commoner
Posts: 69
Joined: Mon Apr 20, 2009 3:40 pm
Location: United Kingdom

Using PHP OOP

Post by lshaw »

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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Using PHP OOP

Post by social_experiment »

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
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Using PHP OOP

Post by Eran »

Use a class only when you understand how using a class helps you. Otherwise you gain nothing, you might as well use a function.
lshaw
Forum Commoner
Posts: 69
Joined: Mon Apr 20, 2009 3:40 pm
Location: United Kingdom

Re: Using PHP OOP

Post by lshaw »

thankyou for your replies

lewis
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Using PHP OOP

Post by Christopher »

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)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Using PHP OOP

Post by Luke »

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