List Info

Thread: global variables in php4 code




global variables in php4 code
user name
2007-10-08 07:41:26
Hi,

hmm .. i am just looking at:
http://cvs.php.net/viewvc.cgi/pear/Text_Password/P
assword.php? 
view=annotate

that global $_Text_Password_NumberOfPossibleCharacters var
definition  
seems incorrect to me. it should use $GLOBALS explicitly as
otherwise  
it might cause issues if included inside a function.
thoughts? i only  
wanted to look at the implementation, i did not really try
out the code.

regards,
Lukas

-- 
PEAR Development Mailing List (http://pear.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


Re: global variables in php4 code
user name
2007-10-08 07:59:52
On 10/8/07, Lukas Kahwe Smith <mlspooteeweet.org> wrote:
>
> that global $_Text_Password_NumberOfPossibleCharacters
var definition
> seems incorrect to me. it should use $GLOBALS
explicitly as otherwise it
> might cause issues if included inside a function.
thoughts?
>

I'm curious about what possible problems this raises.  Some
PHP4 code I have
to work on includes several global variables, that are
constantly being
referenced everywhere.  I've somewhat avoided it, in that
I'll wrap a
particular global with a get() method, and in all the
remaining functions
just use what that get() returns, rather than referencing
the global
variable directly anywhere else but inside the get() method.
 So, if there
are gotchas out there in referencing a global variable like

global $MyGlobal;

without using the $GLOBALS array, I need to understand.
-- 
CRB

Let me introduce you to my very own DMCA-protected
encryption key:
BC 1B 64 4A 8D DE 49 E8 C3 7D CC EE 1A AD EE F5
(compliments of Freedom-to-Tinker http://www.f
reedom-to-tinker.com/?p=1155)
Re: global variables in php4 code
user name
2007-10-08 08:03:20
On 08.10.2007, at 14:59, Chuck Burgess wrote:

> On 10/8/07, Lukas Kahwe Smith <mlspooteeweet.org> wrote:
>>
>> that global
$_Text_Password_NumberOfPossibleCharacters var definition
>> seems incorrect to me. it should use $GLOBALS
explicitly as  
>> otherwise it
>> might cause issues if included inside a function.
thoughts?
>>
>
> I'm curious about what possible problems this raises. 
Some PHP4  
> code I have
> to work on includes several global variables, that are
constantly  
> being
> referenced everywhere.  I've somewhat avoided it, in
that I'll wrap a
> particular global with a get() method, and in all the
remaining  
> functions
> just use what that get() returns, rather than
referencing the global
> variable directly anywhere else but inside the get()
method.  So,  
> if there
> are gotchas out there in referencing a global variable
like
>
> global $MyGlobal;
>
> without using the $GLOBALS array, I need to
understand.

the problem is that if you require the code inside the
function, then  
the variable is not really global, but instead is defined
inside the  
function scope. so if you then call a function from outside
of that  
function call, then the "global $foo" inside the
methods of the  
package will not actually import the variable you expected.

regards,
Lukas

-- 
PEAR Development Mailing List (http://pear.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


Re: global variables in php4 code
user name
2007-10-08 08:12:18
On 10/8/07, Lukas Kahwe Smith <mlspooteeweet.org> wrote:
>
> but instead is defined inside the function scope


I thought that this usage indicated that the actual global
variable
declaration was done somewhere else, and the only way to use
the global from
inside your class/method block was to mention it via the
"global" command,
almost like it really meant "import $MyGlobal". 
So, my understanding was
the variable is being declared/set elsewhere, and is only
being imported
into the class/method block via the "global"
command.

How does ->  global $MyGlobal;

differ from ->  global $GLOBALS['MyGlobal'];

in this scenario?

Is there a danger that you've inadvertently redeclared
$MyGlobal somewhere
in your local code, not intending for it to replace
$GLOBALS['MyGlobal'],
meaning it's possible that both could exist separately?
-- 
CRB

Let me introduce you to my very own DMCA-protected
encryption key:
BC 1B 64 4A 8D DE 49 E8 C3 7D CC EE 1A AD EE F5
(compliments of Freedom-to-Tinker http://www.f
reedom-to-tinker.com/?p=1155)
Re: global variables in php4 code
user name
2007-10-08 08:19:32
On 08.10.2007, at 15:12, Chuck Burgess wrote:

> On 10/8/07, Lukas Kahwe Smith <mlspooteeweet.org> wrote:
>>
>> but instead is defined inside the function scope
>
>
> I thought that this usage indicated that the actual
global variable
> declaration was done somewhere else, and the only way
to use the  
> global from
> inside your class/method block was to mention it via
the "global"  
> command,
> almost like it really meant "import
$MyGlobal".  So, my  
> understanding was
> the variable is being declared/set elsewhere, and is
only being  
> imported
> into the class/method block via the "global"
command.
>
> How does ->  global $MyGlobal;
>
> differ from ->  global $GLOBALS['MyGlobal'];
>
> in this scenario?
>
> Is there a danger that you've inadvertently redeclared
$MyGlobal  
> somewhere
> in your local code, not intending for it to replace
$GLOBALS 
> ['MyGlobal'],
> meaning it's possible that both could exist
separately?

the main problem is the definition of the variable. but i
also think  
there is potential issues when importing the variable. i do
not  
remember exactly, because i moved to using $GLOBALS
everywhere long ago.

regards,
Lukas

-- 
PEAR Development Mailing List (http://pear.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


Re: global variables in php4 code
user name
2007-10-08 09:17:53
According to the PHP docs [1]:

"Notice how $GLOBALS exists in any scope, this is
because $GLOBALS is
a superglobal." As I understand it there is no need to
do a "global
$GLOBALS". This is just a note. I understood the
context of your
question...

The manual only points that specific problem with the use of
"global" :

#####

The Zend Engine 1, driving PHP 4, implements the static and
global
modifier for variables in terms of  references. For example,
a true
global variable imported inside a function scope with the
global
statement actually creates a reference to the global
variable. This
can lead to unexpected behaviour which the following
example
addresses:

<?php
function test_global_ref() {
   global $obj;
   $obj = &new stdclass;
}

function test_global_noref() {
   global $obj;
   $obj = new stdclass;
}

test_global_ref();
var_dump($obj);
test_global_noref();
var_dump($obj);
?>

Executing this example will result in the following output:

NULL
object(stdClass)(0) {
}

#####

regards,
iGor.

[1] http://br.php.net/global


-- 
PEAR Development Mailing List (http://pear.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


[1-6]

about | contact  Other archives ( Real Estate discussion Medical topics )