ricky spires Posted October 6, 2011 Share Posted October 6, 2011 hello, ok if i set a Global on index.php (wwwroot/admin/index.php) it works $page1='hello from page1 on index'; global $page1; echo $page1; i can echo $page1 on other pages like (wwwroot/admin/pages/home.php) and it will echo out "hello from page1 on index". GREAT but if i write a Global on (wwwroot/admin/pages/home.php) it does not work. i also can not echo it out on any other page $page2='hello from page2 on home'; global $page2; echo $page2; why is this ??? Quote Link to comment https://forums.phpfreaks.com/topic/248541-setting-a-global-help/ Share on other sites More sharing options...
Buddski Posted October 6, 2011 Share Posted October 6, 2011 The use of global as no affect on the code unless inside a function declaration. If you can provide a more thorough example, I might be able to assist. Quote Link to comment https://forums.phpfreaks.com/topic/248541-setting-a-global-help/#findComment-1276391 Share on other sites More sharing options...
ricky spires Posted October 6, 2011 Author Share Posted October 6, 2011 ok. so if i had this code below on a page... $pName = 'adminHome'; $page = Pages::find_by_pageName($pName); and this method (function) inside my class public static function find_by_pageName($pName){ $sql = "SELECT * FROM ".self::$table_name." WHERE pageName='".escape_value($pName)."'"; $result_array = self::find_by_sql($sql); return !empty($result_array) ? array_shift($result_array) : false; } which will return these values on the page $page = Pages::find_by_pageName($pName); echo $page->id."<br />"; echo $page->pageName."<br />"; echo $page->visible.'<br/>'; echo $page->layoutElements_id.'<br/>'; echo $page->layoutTemps_id.'<br/>'; echo $page->module_id.'<br/>'; echo $page->title.'<br/>'; echo $page->sub_title.'<br/>'; echo $page->description.'<br/>'; echo $page->image.'<br/>'; echo $page->about.'<br/>'; how would i put the $page->pageName and the $page->layoutTemps_id ( OR ALL OF THEM) into there own globals to use them out on other pages Quote Link to comment https://forums.phpfreaks.com/topic/248541-setting-a-global-help/#findComment-1276401 Share on other sites More sharing options...
trq Posted October 6, 2011 Share Posted October 6, 2011 Globals are inherently bad and should be avoided wherever possible. They completely break the encapsulation provided by functions and classes. Quote Link to comment https://forums.phpfreaks.com/topic/248541-setting-a-global-help/#findComment-1276405 Share on other sites More sharing options...
Buddski Posted October 6, 2011 Share Posted October 6, 2011 The $page variable should be able to be accessed by any locally included file as long as the file is included below the declared variable. // index.php $pName = 'admin'; $page = Pages::find_by_pageName($pName); include('something.php'); Then // something.php echo '$page is a ' , gettype($page); The output of above should be 'object' Quote Link to comment https://forums.phpfreaks.com/topic/248541-setting-a-global-help/#findComment-1276409 Share on other sites More sharing options...
ricky spires Posted October 6, 2011 Author Share Posted October 6, 2011 thanks. i put this code on admin/pages/home.php $pName = 'adminHome'; $page = Pages::find_by_pageName($pName); //include('../index.php'); and i put this code on admin/index.php echo '$page is a ' , gettype($page); and it echo'd back $page is a NULL also i commented out //include('../index.php'); because it just displayed the index.php page on home.php why is $page is a NULL i also tried this echo '$page is a ' , gettype($page); echo $page->pageName."<br />"; but it didnt echo the pageName thanks rick Quote Link to comment https://forums.phpfreaks.com/topic/248541-setting-a-global-help/#findComment-1276438 Share on other sites More sharing options...
trq Posted October 6, 2011 Share Posted October 6, 2011 You cannot make a variable global across different scripts. You will need to include the script that defines $page anywhere you need it. Quote Link to comment https://forums.phpfreaks.com/topic/248541-setting-a-global-help/#findComment-1276591 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.