Another Reason To Use Classes Instead of Functions

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
User avatar
volomike
Forum Regular
Posts: 633
Joined: Wed Jan 16, 2008 9:04 am
Location: Myrtle Beach, South Carolina, USA

Another Reason To Use Classes Instead of Functions

Post by volomike »

Here's reason # 1001 to use a class instead of a list of functions:

A function without a class container or a namespace is kind of like a global variable. You read a passage of code, see this function, and go, whoa! where did this baby come from? Is this some new PHP function I never heard of yet? At least with a class you have some kind of understanding what it belongs with and might be able to find the class file usually.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Another Reason To Use Classes Instead of Functions

Post by josh »

I don't think thats a very good reason to use classes. But maybe thats why you numbered it 1,001 in your book? Where are the other 1,000? heh

functions are a language feature. I don't think a language feature is a smell, its the programmer him/herself, NOT the language that deserves the blame/credit most of the time.

A stand alone function is not always "like global data", however if that function is naturally coupled with other functions AND they share state, then you have a rational reason to use objects.

And classes that use all static members are no better then a function using globals. In fact all you did was organize your code, you change nothing about the "global" properties of it by simply moving the same function to a class.

So in short

use of functions != global data
bad programmers == use of global data in functions
use of classes != avoiding global state
Post Reply