Webmaster Forum

Go Back   Webmaster Forum > Web Development > Coding Forum

Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more.


 
 
LinkBack Thread Tools Display Modes
Prev Previous Post   Next Post Next
Old 06-26-2009, 07:06 AM   #1 (permalink)
Meeow!
 
Costin Trifan's Avatar
 
Join Date: 04-13-07
Location: Romania
Posts: 3,235
iTrader: 0 / 0%
Latest Blog:
None

Costin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest orderCostin Trifan is a web professional of the highest order
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
Costin Trifan is offline  
Add Post to del.icio.us
Reply With Quote
Go Back   Webmaster Forum > Web Development > Coding Forum

 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
IP Block or Adsense Filtering Grant Draper Webmaster Revenue 0 09-28-2008 09:58 AM
Google Filtering Links theChronic Marketing Forum 6 02-18-2007 12:29 AM
Google is filtering doorway pages. newsguy Google Forum 3 11-19-2004 04:14 PM


Sponsor Links
Get exposure! Contextual Links V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 11:23 PM.
© Copyright 2008 V7 Inc
Powered by vBulletin
Copyright © 2000-2009 Jelsoft Enterprises Limited.


Search Engine Optimization by vBSEO 3.3.0 ©2009, Crawlability, Inc.