Sin: Globals in OO PHP

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

How big of a sin is using globals in OO PHP?

1
0
No votes
2
0
No votes
3
0
No votes
4
1
11%
5
4
44%
huh?
4
44%
 
Total votes: 9

Porter
Forum Newbie
Posts: 5
Joined: Thu Jul 04, 2002 1:30 am
Location: Clearwater, FL
Contact:

Sin: Globals in OO PHP

Post by Porter »

I'd like to know what you guys think about using globals in OO PHP. I've thought about it for a while and asked a few people. Some claim it's how they were taught. Some, like me, claim it to be a huge sin. The poll is from 1 to 5(because I can't have it go up to 10). 1 being the lowest and 5 being the highest. huh? being you have no clue what I'm talking about.

Oh, I'm new here and it seems to be a great place to get help. I'm sure I'll stick around.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

The whole point of OOP is to make your functions and variables localized to the object. The idea of encapsulation is very powerful and is the driving force behind the existence of OOP. Globals can ALWAYS be avoided.

In fact, the only time a global should be used is if it is a global constant. No variable should be global. If it is global, then you have a bad design. Period.
Porter
Forum Newbie
Posts: 5
Joined: Thu Jul 04, 2002 1:30 am
Location: Clearwater, FL
Contact:

Post by Porter »

Glad to hear that you agree with me :D
Ever heard of Invisionboard? I'm sort of...how do you say, completely against it. I've seen more and more people using globals with oo php and I think it might just have somethign to do with it. The funny thing is that most people that use globals it that they use globals in the same way they would be using methods. It's really funny.
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

While it is bad design, and not elegant to use globals in a completely OOP environment, wouldn't it be acceptable if one was tuning a program strictly for performance, and didn't want object overhead every time they needed the variable?
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Globals and OOP

Post by BDKR »

Well, I too agree that Globals are prolly a bad idea in a purely OO environ, BUT, PHP isn't an ideal language to take a purely OO aproach. It's implementation of OO is lacking. Growing, yes, but lacking still.

However, I'm not religous about how things are ultimately implemented. You shoot for the very best, but optimize or tweak based on what's before you. And PHP OO is a bag full of work arounds.

But PHP still kicks butt!

Later on,
BDKR (TRC)
Post Reply