06-26-2009, 07:06 AM
|
#1 (permalink)
|
|
Meeow!
Join Date: 04-13-07
Location: Romania
Posts: 3,235
Latest Blog: None
|
any way of filtering $GLOBALS ?
I'm working on a big project I can't give you too much details about this right now, sorry, but I need a way of filtering the $GLOBALS variable.
The current architecture is a template based one so I need a way to extract only some of the global variables into some of my scripts.
Although it might seem easy I really have a big problem in getting it to work(maybe I have too much on my head right now and I obviously can't think clearly enough) so I need some help.
Description:
I have a php file, let's call it < main.php > which will hold some of the configuration settings and import some other scripts as well. Basically this would be the website's main file. It might look like this:
PHP Code:
<?php // main.php
// Globals
$server = ''; $db = ''; $user = ''; $password = ''; $cs = array( 'server' => $server, 'db' => $db /* etc */);
$sql = new MySqlWrapper(); require 'lang.php'; $lang = array( 'welcome_msg' => 'Welcome to our website!' ,'home_page_title' => 'Welcome!' );
require 'tpl.php'; $tpl = new TemplateClass()
require 'other_class.php'; $class = new OtherClass();
/* etc ... */ ?>
Then I need to access some of these variables in other external pages, an example would be the < functions.php > file that will be included in some of the website's pages:
PHP Code:
<?php // functions.php
$tpl->set_var('welcome_msg', $lang['welcome_msg']); $tpl->set_var('home_page_title', $lang['home_page_title']);
// ^^ breaks the script. $tpl and $lang variables are not visible on this page // an alternative would be extract(GLOBALS); but that would bring on ALL globals here and I don't need them all ?>
And finally, an external page would look something like this:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>{home_page_title}</title>
</head>
<body>
<h1>{welcome_msg}</h1>
</body>
</html>
Now, my problem is accessing the global variables set in main.php file in the functions.php file, so I need to filter the $GLOBALS variable and I tried a few tricks but I couldn't get what I wanted.
The point of this is that the $GLOBALS variable is holding everything so I need to have the function exclude_globals() to filter them out,
PHP Code:
/** * Filter $GLOBALS * * @param string|array $exclude The variable or the array of variables to exclude * @return array */ function exclude_globals( $exclude = NULL ) { # Will hold the $GLOBALS filtered $new = array();
# the default list of variables to exclude from $GLOBALS $default_excludes = array('_POST', '_GET', '_COOKIE', '_FILES', '_SESSION', '_SERVER');
if (is_array($exclude)) { $excludes = array_merge($default_excludes, $exclude); } else { array_push($default_excludes, $exclude); }
// TODO: //....
return $new; }
function that I would call in my functions.php file
PHP Code:
<?php // functions.php
$globals = exclude_globals(); extract($globals);
$tpl->set_var('welcome_msg', $lang['welcome_msg']); $tpl->set_var('home_page_title', $lang['home_page_title']); ?>
and give me access to the global variables set in the other included files.
Can anyone help?
__________________
...to be continued
|
|
|