Benefits of Classes

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
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Benefits of Classes

Post by Inkyskin »

Hi all, I have a question - I know that classes are useful for plugging into many different projects without much or any modification etc, but do they have any benefit over a standard set of functions that you may have on every page via an include? Are there any speed or load savings?

I can't seem to find any information anywhere that discusses the benefits of classes...
Bon Bon
Forum Commoner
Posts: 66
Joined: Sat Mar 13, 2004 10:21 pm
Location: UK

Post by Bon Bon »

I use classes so that I get paid more at work by my boss, it also prevents him from having any understanding at all of how things work. This benefits me as it means I can do what the hell I want as long as the job gets done. :lol:
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

Being reusable is one the main avantages of a class. You can also extend the functionality of a existing class.
A well written class can be reusable in many projects and be fast as same time. Depends much on your skills.
Classes vs functions is a very debate subject on the internet I' m curios how you didn' t find anything.
Each has it' s area where is strongest:
functions in mediums where is need that a lot information is passed very fast between client and server (includes server talking to each other), a simple website;
classes where the code can be reused, extended, etc like in big projects or a complex application .
Last edited by Rovas on Wed Dec 05, 2007 5:26 am, edited 1 time in total.
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Post by Inkyskin »

Thanks, thats a lot clearer now - most of the information I could find was just about how to write a class, not discussing it's merits.
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

Bon Bon post is very enlightning.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

For you to understand the usefulness of classes, you should understand what OOP is. OOP means that your application is based on objects with attributes and capabilities. OOP became popular because it allows us to think in a more worldly mindset. With procedural programming, you think in terms of actions and commands. With OOP, you think in terms of communication and responsibilities.

Also, if you look into object-oriented design (not programming, just design) you'll notice that classes can solve all sort of problems with the use of design patterns.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

The most important thing about classes is that first and foremost they are DATA. You can achieve a bad replica of a class by passing a struct around to a library of functions. But classes create a namespace with variables that are private to that library of functions. That data protection, which follows the same idea as not using global variables, is central to classes.

When you develop using OO you should be thinking of the data first, then write functions to work on that data.

The next step is to think of the result/behavior you want first, second write a test that specifies it, then define the data, and finally the code.
(#10850)
Post Reply