Jump to content

Bizarre Phenomenon


ShadowMage

Recommended Posts

Hey guys I've a strange issue. Seems that by moving some global variables around they get lost?!? Not sure how. This might be a little hard to explain but here goes.I have a class called Skylight and it has several child classes. I've declared some global variables that will be used both in methods of the classes and outside the scope of the class. I also have an included file. Here's the setup:

<?phprequire_once('./IncludeFiles/InitializeArrays.php');$AssemblyPartCnt = 3; //The number of parts listed in an assembly, used as bound in for loops to get part numbers$PaintIndex = 2; //Index number for paint charge in Finishes table?>

And create the skylight like so:

<?php....//Create the skylight object$tmpSource = null;if (!$blnNewSet) {	if (count($arrSkylightDtl) == 1) {		$tmpSource = $arrSkylightDtl[0];	}}setVariables($tmpSource);$skylight = createSkylight($postType, "./Classes");//Then write the HTMLif (isset($skylight)) {	echo $skylight->WriteHTML(0);	//echo $skylight->WriteHTML(1);} else {	echo "<br /><b>Error:</b> Could not find "".$postType."" for quote ".$postQuoteNum."<br />";}?>

Now when I try to use those within the WriteHTML method, I get an undefined index. Here's the code for that:

$value = $arrFinishes[$PaintIndex]['CostPerFt'];if ($this->FinishCharge != '') {	$value = $this->FinishCharge;}

Now the weird thing is that if I move the variables inside the included file (I have other variables in that file that are used globally, which is why I decided to try moving them in there) then they work. Can anybody explain that?

Link to comment
Share on other sites

Variables that are not part of functions are not available within functions. You need to explicitly import them with the global keyword, like:

function doSomething() {global $AssemblyPartCnt;}

I'm not entirely sure if this can also be used in class methods, but either way - it would be "cleaner" if you pass those at the constructor.

Link to comment
Share on other sites

Variables that are not part of functions are not available within functions. You need to explicitly import them with the global keyword, like:
function doSomething() {global $AssemblyPartCnt;}

I'm not entirely sure if this can also be used in class methods, but either way - it would be "cleaner" if you pass those at the constructor.

I did import them with global.
public function WriteHTML($hover, $setID='') {	global $AssemblyPartCnt, $PaintIndex, $arrAssemblies, $arrGlazeType, $arrGlazing, $arrFinishes, $arrBrMetal, $arrInsulation, $arrEngCheckNames;

In the above code, all the variables starting with $arr are declared and initialized within the InitializeArrays.php includes file. I can access those arrays perfectly fine without any errors. $AssemblyPartCnt and $PaintIndex are the ones throwing a fit. If I declare them in InitializeArrays.php, then they don't want to work, but if I declare them just after my include line (where I include InitializeArrays.php) then I can access them and I don't get any errors.As for passing them in the constructor, I have a long list of those arrays which means I would have an equally long list of parameters to pass in through the constructor. So I think it might be slightly 'cleaner' to do it with the global keyword. Or are you suggesting there might be performance benefits?EDIT: Well, my issues seems to be resolved. :) Which is strange because I haven't changed anything and it sure wasn't working last week! Hmm........

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...