I have a problem with using globals within class scope.

For example:

$GLOBALS['test'] = "How are you?";

class great
{
[INDENT]function grace() {
[INDENT]
echo $test;
echo $GLOBALS['test'];
[/INDENT]}
[/INDENT]}

Problem is, that neither of the echos produce any output. Is there a problem with accessing superglobals from within classes? I've tried this with two separate classes and they both have the same result. Not too sure if there is a need to adjust the RegisterGlobals setting. Is there a way I can get by without changing this setting?

I think you don't set a value in a superglobal.

You set a value in a variable and use $GLOBALS to retrieve it.

<?php
$test = 'superglobal $test';

grace();

function grace() {
	$test = 'local $test';
	
	echo "$test <br>";
	echo $GLOBALS['test'];
}
?>

I'm not sure in your case (in classes).

But give it a try and let us know.

tried and failed

How are you using the class?

I'm using static methods from the class (prob didn't say that during the question!) Apart from that, its how its outlined on the code above, with a call to that static function. The function executes, but the globals are not being carried accross.

This is something I tried and works:

<?php
$test = 'global works!';

class great {
	function grace() {
		return $GLOBALS['test'];
	}
}

$one = new great();

echo $one->grace();
?>

Doesn't seem to be working on mine. Here's the run:

$_lxml = simplexml_load_file('config.xml');

// Tried with this line
$_xml = $_lxml;

// And on a separate test, tried this line
$GLOBALS['_xml'] = $_lxml;

$_session = new HSession();
$_session->applysession("username", "password");

class HSession {

public function applysession($username, $password)
{
echo $GLOBALS['_xml'];
}
}

You didn't close your "password" correctly.

I tested this which worked:

<?php
$_lxml = "text";

// Tried with this line
$_xml = $_lxml;

$_session = new HSession();
$_session->applysession("username", "password");

class HSession {

public function applysession($username, $password)
{
echo $GLOBALS['_xml'];
}
}
?>

I found the problem,

I was using echo instead of print_r for the output, so my test went out the window.
because $_xml was an xml document, echo didn't produce any output, but print_r does.

Thankyou

Great to know that. Have a smooth day ahead!

will do :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.