0

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?

Edited by mingler: n/a

2
Contributors
10
Replies
11
Views
6 Years
Discussion Span
Last Post by mingler
0

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.

0

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.

0

This is something I tried and works:

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

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

$one = new great();

echo $one->grace();
?>
0

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'];
}
}

Edited by mingler: n/a

0

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'];
}
}
?>

Edited by Javvy: n/a

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.