Jump to content

Having difficulty with variable scope of required files in a class


Traxus

Recommended Posts

I'm having a problem with (what I'm almost certain is) variable scope, maybe you can help...

 

So I've got a parent class that, among other things, gets the name of the filepath, explodes this filepath, checks for files with names that mirror that filepath (without the slashes of course), and includes them if they exist:

 

 

class ParentClass {

  public $RootFolder;
  public $PageSections;

  function __construct() {
	global $root_folder;
	$this->RootFolder = $root_folder;
	//get all directory names...
	$cleanse = $this->RootFolder;
	$pagepath = str_replace($cleanse, '', dirname($_SERVER['PHP_SELF']));
	$this->PageSections = explode('/', $pagepath);
  }

  function RequireIfExists ($sections, $root_folder, $file_name, $file_type) {
	foreach ($sections as $i){
		if (isset($o)) {
			$i = $o."/".$i;
		}
		$file = $root_folder.$i."/".$file_name.$file_type;
		if (file_exists($_SERVER['DOCUMENT_ROOT'].$file)) {
			require ($_SERVER['DOCUMENT_ROOT'].$file);
			echo "<!--".$file." included -->"; //temporary measure to ensure that the file is at least being called.
		}
		$o = $i;
	}
  }
// so, excuting the function
// $this->RequireIfExists($this->PageSections, $this->RootFolder, "sectionvars", ".php");
// on some/sub/folder/index.php
// will look for somesectionvars.php, somesubsecitonvars.php & somesubfoldersectionvars.php
// and include whichever ones exist.
}

 

Then there is a child class that utilizes this function, and successfully requires the file. I know the file is coming through because the echo statement on the page shows up in the code, but I cannot get the variables to pass through?

 

<?php
class HtmlHead extends PageBlock{
public $PageKeywords;
public $PageDescription;
public $PageStyles;
public $PageScripts;
//------------------------
public $SectionKeywords;
public $SectionDescription;
//------------------------
public $SiteKeywords;
public $SiteDescription;
public $SiteStyles;
public $SiteScripts;
//------------------------
public $CatchAll;

  function __construct() {
	parent::__construct();
	//$this->RequireIfExists($this->PageSections, $this->RootFolder, "sectionvars", ".php"); 		//I have tried calling the function here with no luck
	global $site_description, $site_keywords, $section_description, $section_keywords, $xyz;			//also made sure to global the variables from the required page
	$this->SiteDescription = $site_description;
	$this->SiteKeywords = $site_keywords;
	$this->SectionDescription = $section_description;
	$this->SectionKeywords = $section_keywords;
	$this->PageDescription = $page_description;
	$this->PageKeywords = $page_keywords;
  }

function Constructor() {
	$this->RequireIfExists($this->PageSections, $this->RootFolder, "sectionvars", ".php");	//Tried calling the function here as well, no luck
	global $xyz; //global'd the var i want
	echo "\n<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />";
	echo "\n<!--Sec Keywords are".$xyz."-->";	//Tried to echo the var...
	//------------------------Rest of code...
}
}

 

And here is the page that is being required...

 

<?php
echo "<!-- 1234 1234 1234 1234 -->"; //test measure to see if/where file is including
$section_keywords = "red, secondary";
$section_description = "this is the red section description";
$xyz = "1234"; //test Var to see if there was a naming conflict with the above two..
?>

 

What am I overlooking this time?  :wtf:

Link to comment
Share on other sites

You know when you spend 20 minutes writing a post and you figure out the answer 20 seconds after?

 

I looked into $GLOBALS['var] on the included page. Worked. FML.

 

<?php
echo "<!-- 1234 1234 1234 1234 -->"; //test measure to see if/where file is including
$section_keywords = "red, secondary";
$section_description = "this is the red section description";
$GLOBALS['xyz'] = "1234"; //test Var to see if there was a naming conflict with the above two..
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.