Page 1 of 1

Frustrating error with global variable...

Posted: Tue Mar 30, 2010 8:53 pm
by jraede
I've been working all day, so this is probably a silly mistake, but I can't figure out why this isn't working any more.

Basically, I set a variable called $nf_listing, which is an object of a particular class. I have functions that declare the variable like "global $nf_listing", and then run various methods on that class. Kinda like WordPress, if you guys have used that.

My problem is, the functions aren't recognizing the global variable as being set. Here's my code:

Code: Select all

while(has_results()) : 
	$nf_listing = create_object(); 
endwhile;


require ROOT_DIR . "/includes/functions-internal-event.php";
require ROOT_DIR . "/includes/functions-display-event.php";
require ROOT_DIR . "/includes/functions-internal-venue.php";
require ROOT_DIR . "/includes/functions-display-venue.php";
require ROOT_DIR . TEMPLATE_DIR . '/event.php';
When I call one of the functions from functions-display-event.php, I get an error "cannot call this method on a non-object", so clearly it's not seeing that $nf_listing is an object. But, in the first line of each function in functions-display-event.php, I have "global $nf_listing;". Also, I know that I'm setting the object correctly in the above code, because when I add a line "echo $nf_listing;", even outside of the while loop, I get that error, "cannot convert object to string".

Example function in functions-display-event.php:

Code: Select all

function event_title($link = TRUE) {
	global $nf_listing;
	$nf_listing->event_title($link);
}
Any ideas? Thanks.

Re: Frustrating error with global variable...

Posted: Tue Mar 30, 2010 9:11 pm
by Christopher
What is printed if you do:

var_dump($nf_listing);

Re: Frustrating error with global variable...

Posted: Tue Mar 30, 2010 9:23 pm
by jraede
I run it on that main page and I get what's expected, all of the internal variables set on that object. Running it within the functions, as expected, I get NULL.

I narrowed everything down and simplified the error. Here's what I have now:

Code: Select all

while(has_results()) : 
	$nf_listing = create_object(); 
endwhile;
echo "<h1>Outside Function</h1>";
var_dump($nf_listing);
function test_function() {
	global $nf_listing;
	echo "<h1>Within Function</h1>";
	var_dump($nf_listing);
}

test_function();
exit();
I'm getting all the variables in the "Outside Function" dump, but "NULL" in the "Within Function" dump.

Re: Frustrating error with global variable...

Posted: Tue Mar 30, 2010 9:24 pm
by phu
You may be running into a scope issue.

Try setting:
$nf_listing = false;

Or something similar before that while loop happens.

Re: Frustrating error with global variable...

Posted: Tue Mar 30, 2010 9:38 pm
by jraede
Still no...this is especially weird because those functions has_results() and create_object() do the exact same thing, but with a different global variable called $application. That variable is the application instance I set on index.php of my MVC framework, and $nf_listing is the variable I set after the bottom-layer class calls an initializer file (which is the one I've shown you), which then calls the template file. Basically I run functions like listing_title() and listing_date() on $nf_listing within the template.

The only thing that I can think of is that I messed around with php.ini for a second, but changed everything back when I found an alternative solution to what I was trying to do...regardless, that would have affected both of these global variables.

Here's what I have now, still not working:

Code: Select all

$nf_listing = FALSE;
while(has_results()) : 
	$nf_listing = create_object(); 
endwhile;
echo "<h1>Outside Function</h1>";
var_dump($nf_listing);
function test_function() {
	global $nf_listing;
	echo "<h1>Within Function</h1>";
	var_dump($nf_listing);
}

test_function();
exit();

Re: Frustrating error with global variable...

Posted: Tue Mar 30, 2010 9:44 pm
by jraede
Would it potentially be a problem if all of this code is still technically being run within the scope of my global application object? IE, there are methods within that object that call the init file, so the template file is being called while still within that scope. I can substitute out $this-> for $application-> and it works the same way. Just a thought..

Re: Frustrating error with global variable...

Posted: Tue Mar 30, 2010 9:44 pm
by phu
The following returns the expected values ("testing" and "testing").

Code: Select all

$nf_listing = false;
$has_results = true;

while($has_results)
{
   $nf_listing = 'testing';
   $has_results = false;
}

echo "<h1>Outside Function</h1>";
var_dump($nf_listing);

function test_function() {
   global $nf_listing;
   echo "<h1>Within Function</h1>";
   var_dump($nf_listing);
}

test_function();
I would suggest that something is wrong within create_object(), and that debug output within that and testing on its return value would be valuable.

Re: Frustrating error with global variable...

Posted: Tue Mar 30, 2010 10:00 pm
by jraede
Ok, let me post my has_results() and create_object() functions so you can see what's going on. As far as I can tell, they're working fine. However, it could be because that create_object() is a function, and creating the object within the function, so that may affect the scope. Although, I don't know enough about scopes to know if that's the case or not.

Code: Select all

public function do_query($query_string) {
		if(isset($this->results_per_page)) {
			// Append the pagination string
			$paginate = $this->paginate();
		}
		$orderby = $this->set_order();
		$this->results = mysql_query($query_string.$orderby.$paginate);
		$this->num_results = mysql_num_rows($this->results);
	}

public function has_results() {
		if($this->cur_row < $this->num_results) {
			return TRUE;
		}
		return FALSE;
	}

public function create_object() {
		$row = mysql_fetch_assoc($this->results);
		$this->cur_row++;
		if($this->has_results()) {
			mysql_data_seek($this->results, $this->cur_row);
		}
		$name = $this->object_class;
		$object = new $name($row);
		return $object;
	}
Like I said, all of this is working fine. But it could be the issue I described above.

Re: Frustrating error with global variable...

Posted: Tue Mar 30, 2010 10:50 pm
by phu
You're posting object code (using $this) with no class context. There's no way for us to know what might or might not be going wrong with it, without using contrived code of our own.

I'm signing off in a few, so hopefully some others around here will help out, but like I said, I'd drop debug output or (preferably) actual warning/error reporting into your code to make sure these methods are doing what you intend. If that's not the case, then I'm guessing it might have something to do with not giving the actual context in which this code is running, since that's how you've posted these methods (without a class).