1
worlds
PHP globals question.
  • 2004/3/19 6:48

  • worlds

  • Just popping in

  • Posts: 61

  • Since: 2004/3/13


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 "

Global scope, \$bookstore_fields_p='$bookstore_fields_p'

";
foreach($GLOBALS as $k => $v)
echo "

GLOBAL: $k => $v

";

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

2
Mithrandir
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.

3
worlds
Re: PHP globals question.
  • 2004/3/19 18:03

  • worlds

  • Just popping in

  • Posts: 61

  • Since: 2004/3/13


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


-----------------------------------
// 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;
}
?>


4
worlds
Re: PHP globals question. _solved
  • 2004/3/22 19:49

  • worlds

  • Just popping in

  • Posts: 61

  • Since: 2004/3/13


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
http://www.worldware.com

5
Mithrandir
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.

6
mvandam
Re: PHP globals question. _solved
  • 2004/3/22 21:03

  • mvandam

  • Quite a regular

  • Posts: 253

  • Since: 2003/2/7 2


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.

Login

Who's Online

443 user(s) are online (310 user(s) are browsing Support Forums)


Members: 0


Guests: 443


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits