Strange Behaviour when working with Class Variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DFAlpha
Forum Newbie
Posts: 5
Joined: Sun May 11, 2003 12:28 am

Strange Behaviour when working with Class Variables

Post by DFAlpha »

Hey guys and gals,

I've got a problem that I've been working on for a few hours with no solution thusfar. I'll try to explain below.

I have a file, lets say vars.php. Inside that is code like the follows:

Code: Select all

//vars.php
$tmp = 120;
$tmp2 = 160;
Now, I have another file, called say loadVars.php. Although these names may seem abstract here, they fit in with a much larger project. Inside this file is a class, the code is something like this:

Code: Select all

//loadVars.php
class loadVars {
var $tmpVar;

function loadVars() { 
include 'vars.php';
$this->tmpVar = $tmp;
}}
I have a third file, say called outputData.php. The code looks like this...

Code: Select all

//outputData.php
class outputData {
function outputData(&$testRef) {
echo $testRef->tmpVar;}}
Now, in the main file, the code looks like this...

Code: Select all

//Main execution stream
include(loadVars.php);
include(outputData.php);
$testObject = new loadVars();
$output = new outputData($testObject);
The problem is, sometimes the code outputs the correct info (it should output 120), but sometimes it outputs nothing, it changes. This is a really odd behaviour, something that is extremely strange, ive never seen the result change when you run it back to back times and get different results, really odd.

If anyone has any suggestions, I would greatly appreciate it.

Regards,
Shawn
DFAlpha
Forum Newbie
Posts: 5
Joined: Sun May 11, 2003 12:28 am

Post by DFAlpha »

It appears that the problem lies in loading the $tmp variable from vars.php. It can be easily corrected by making $tmp global, but is there any way to correct it without using global variables?

Thanks,
Shawn
Post Reply