Re: Opinion about the using $GLOBALS directly

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello Tedd,

Here my opinoins

First of all, I ask this question to is there any technical dead end for using 
$GLOBALS directly.

It seems not.

And I believe other arguments was closely connected to coding style.

I'm self learner, I learn evrything about computers, programming (that means 
PHP) and English by myself. Because of this time to time I found my self into 
very alien position for others.

On Monday 24 December 2007 19:13:07 tedd wrote:
> At 4:18 PM +0200 12/19/07, Sancar Saran wrote:
> >  > that said, avoid globals like the plague - sometimes you may come up
> >  > with
> >>
> >>  a situation where using a global is really necessary - such situations
> >>  should be the exception rather than the rule, often if your thinking of
> >>  using a global there is another way of doing it. jmho
> >
> >And this is why I'm asking here, WHY I should avoid globals like plague ?
> >Every one says bad. Alright, but no one says WHY ?
>
> Hi:
>
> I'm a little late to this thread (been busy), but this is why I
> rarely use globals and have not used them in php.
>
> One of the fundamentals of programming I've learned is to reduce
> problem/solution to their most basic form. Simply, you see a problem
> and you solve it by dividing the problem it into smaller parts and
> then writing code to solve the small parts. Eventually, all the small
> solutions come together to solve the larger problem.

Absolutely. I learn this from very hard way. (rewriting the code :))

> Certainly, and it seems even logical, that you can use a global
> variable to communicate between the different parts and everything
> should work -- IF -- that's all there was to it. But, if you have to
> debug the code OR if you want to use a portion of that solution to
> solve a different problem, then you can have difficulties. For
> example:
>
> [1] If you have to debug the code, there's not a really good way to
> look at a function and see if a variable in it is a global or not --
> therein lies a problem, you don't readily know.
>
> In other languages I adopted a naming convention that used "g" as the
> first letter of a Global variable, like gThisIsMyGolbalVariable --
> that way at least I knew the variable was global. But, even then I
> didn't know where the global was defined or where it might be changed
> -- it was just an unknown in my function that I had to consider.

$GLOBALS['myVariable'] directly solves the problem. 

> [2] If you want to reused a portion of the code, then you have to
> also accommodate the global. This makes transporting your code from
> one application to another problematic because you never know IF your
> function replies on a global or not. If you never use globals, then
> that's never a problem.

If is there any possibility to use the function with some other variables I 
use it to.

> So, my advice is to not use globals, but rather make the functions
> independent from globals. That way you troubleshoot them easier and
> can cut/paste them into other solutions without having to worry if
> some part of that function relies on something who knows where.
>
> That's my reasons why I avoid globals.
>
> Cheers,
>
> tedd
>
> --
> -------
> http://sperling.com  http://ancientstones.com  http://earthstones.com

Here Some basic list for my $GLOBALS usage.

$GLOBALS['db'] =  my Adodb object
$GLOBALS['mc'] =  my Memcached object
$GLOBALS['l'] =  my Language keys
$GLOBALS['d'] = my debug pool. 
$GLOBALS['c'] = my config array.

And someting like that.

A very basic function for pooling debugs.

function addDebug($_sDebugVal) {
		$GLOBALS['d']['_DMSG'][] = $_sDebugVal;
}

usage addDebug($MyVar); or addDebug(print_r($arrayFoo,true));

end of page 

getDebug() {
	return $GLOBALS['l']['_debug_messages_']."<br/><pre>".implode("\n".
$GLOBALS['d']['_DMSG'])."</pre>";
}

Regards

Sancar

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux