xoops forums

worlds

Just popping in
Posted on: 2004/3/19 6:48
worlds
worlds (Show more)
Just popping in
Posts: 61
Since: 2004/3/13
#1

PHP globals question.

Hi -

I'm doing something that should be simple. I declare an array in one file, and then include it from several other files.

Works great. Except in the file for my blocks. The first lines of code are identical, and yet, it works when included in my admin/index.php, and index.php, but not from blocks/foo.php. Clearly something I don't understand.

The files (now) start with identical code, (except the path to mainfile.php) to print out all globals. In two modules, I see exactly what I expect. In the third, the global I'm looking for is simply not in the $GLOBALS array.

Now, interestingly, I can print the variable at global scope, but not within the function, even with the global declaration.

I know the file is getting successfully included, since I have an 'echo' statement in the included file.

They all start with:

require "mainfile.php";
require_once(XOOPS_ROOT_PATH ."/modules/bookstore/myfunctions.php");

echo "<P>Global scope, \$bookstore_fields_p='$bookstore_fields_p'</P>";
foreach($GLOBALS as $k => $v)
echo "<P><U>GLOBAL: $k => $v</U></P>";

But only two out of three show the variable I declare.

Anyone want to explain to me what I'm missing? I suspect it's something obvious, but I'm not seeing it.

Tom

Mithrandir

XOOPS is my life!
Posted on: 2004/3/19 10:56
Mithrandir
Mithrandir (Show more)
XOOPS is my life!
Posts: 6320
Since: 2003/6/21
#2

Re: PHP globals question.

How do you declare the array?

I think it is easier to spot it on a live sample of the code.

worlds

Just popping in
Posted on: 2004/3/19 18:03
worlds
worlds (Show more)
Just popping in
Posts: 61
Since: 2004/3/13
#3

Re: PHP globals question.

Thanks for the quick response.

So, forget about the multiple modules. I've attached code for a block below. As it is, it works. If I move $bfp out of the function, and uncomment the global $bfp it doesn't. I'm sure I'm doing something obvious and dumb, but I think that should work.

Of course, I've been programming in PHP for only a few weeks, so my opinion doesn't count for much

I I cut and paste the code into my admin/index.php, it seems to work there. Is there something different about the execution environment of blocks?


Thanks,

Tom


-----------------------------------
<?php
// Gets the value as a string. If the value is an array, it concatenates the elements, separated by commas
function bookstore_get_value($v)
{
if (!is_array($v))
return $v;
$str = "";
$first = true;
foreach($v as $i)
{
if (!$first)
$str .= ", ";
$str .= $i;
$first = false;
}
return $str;
}


function b_bookstore_show()
{
global $xoopsDB;
// global $bfp;
$bfp = array("ProductName", "SalesRank", "Asin", "Url", "ImageUrlMedium", "Authors", "ReleaseDate", "Catalog", "OurPrice", "Manufacturer");

$block = array();
$queryString = 'SELECT ' . bookstore_get_value($bfp) . ' FROM ' . $xoopsDB->prefix('bookstore_product') . " ORDER BY $bfp[0] ASC";
$result = $xoopsDB->query($queryString);
while ($values = $xoopsDB->fetchRow($result))
{
$link = array();
for ($i = 0; $i < count($bfp); $i++)
{
$link[$bfp[$i]] = $values[$i];
}
$block['links'][] = $link;
}
return $block;
}
?>

worlds

Just popping in
Posted on: 2004/3/22 19:49
worlds
worlds (Show more)
Just popping in
Posts: 61
Since: 2004/3/13
#4

Re: PHP globals question. _solved

Hi -

It appears that the problem is that the code for blocks is actually included by XOOPS WITHIN A FUNCTION.

Note that I have not found this in the XOOPS code yet, but it sure seems to behave that way.

The results is that the variables in the included file, that I thought were being included at global scope, actually end up being defined within a function.

Which means that when I tried to access these variable as globals, they weren't there, because they were declared within the enclosing function, not at global scope.

So the fix is to manually put the variables declared in the included files into $GLOBALS, and then things work, whether the code is included a global scope, or within a function

$bookstore_config_info_ = array(...);
$GLOBALS["bookstore_config_info"] = $bookstore_config_info_;

So, I have two questions.
1) If someone knows the XOOPS code better than I can tell me whather my conclusions are correct. (Or just point me to the right part of the code...)

2) Is there a way for a nested function to access the variables declared in the enclosing function? (I'm just curious, this workaround means I don't need to know).

Tom
www.worldware.com

Mithrandir

XOOPS is my life!
Posted on: 2004/3/22 20:07
Mithrandir
Mithrandir (Show more)
XOOPS is my life!
Posts: 6320
Since: 2003/6/21
#5

Re: PHP globals question. _solved

Generally you should work on having blocks being self-enclosed and certainly not have your main module pages being dependant on a block.

Whether you will do this by having a include_once of a function file, which you can then run to get the array (in order to only have the array definition in one place) in both block and main pages, is up to you.

mvandam

Quite a regular
Posted on: 2004/3/22 21:03
mvandam
mvandam (Show more)
Quite a regular
Posts: 253
Since: 2003/2/7 2
#6

Re: PHP globals question. _solved

Just a couple of points:

Quote:

It appears that the problem is that the code for blocks is actually included by XOOPS WITHIN A FUNCTION.

Note that I have not found this in the XOOPS code yet, but it sure seems to behave that way.

You *have* found it. Your function "function b_bookstore_show()" is what generating the template data for the block. (This function is called automatically by the XOOPS core.)

Quote:

So, I have two questions.
1) If someone knows the XOOPS code better than I can tell me whather my conclusions are correct. (Or just point me to the right part of the code...)

To access global variables within PHP, you must always use 'global $var;' before using it. If you are within a function, PHP doesn't magically know you are talking about a global variable. This is a little weird, but this is how PHP works.

e.g.

global $somevar;
$somevar = 'xyz';
function dosomething() {
global $somevar;
// do something with $somevar
}

Quote:

2) Is there a way for a nested function to access the variables declared in the enclosing function? (I'm just curious, this workaround means I don't need to know).

Well, you should probably pass the variable *into* the enclosed function . Global variables are usually a bad idea.