pandyboy Posted February 8, 2010 Share Posted February 8, 2010 I am currently writing some code, in which I have a number of functions which display included files from a content folder, in a similar way to the following: public function myFunction() { $variable1 = "dog"; $array1=array("cat","pig","gnu"); $this->displayHeader(); ob_start(); include ($this->contentFolder.'page1.php'); echo ob_get_clean(); $this->displayFooter(); } In this example, page1.php contains references to the variable and array defined in the function. This works fine, but of course if I wanted to create a function to handle the displaying of the content, similar to the following:- public function showPage($filename) { $this->displayHeader(); ob_start(); include ($this->contentFolder.$filename); echo ob_get_clean(); $this->displayFooter(); } public function myFunction() { $variable1 = "dog"; $array1=array("cat","pig","gnu"); $this->showPage("page1.php"); } ..then the variables would of course not be passed to the showPage function. As the variables and arrays are going to be different each time the function is called, is there any way I can get this to work? I am just trying to streamline my code and also, ultimately, adapt the showPage function to do more such as checking if the file exists, present an alternative if not etc. etc. I'm open to suggestions. Quote Link to comment https://forums.phpfreaks.com/topic/191337-functions-includes-and-variables-a-poser/ Share on other sites More sharing options...
alexjb Posted February 8, 2010 Share Posted February 8, 2010 If you haven't set it up as a class and you don't want to pass each variable individually between the functions, you might want to look in to using global variables. http://php.net/manual/en/language.variables.scope.php Quote Link to comment https://forums.phpfreaks.com/topic/191337-functions-includes-and-variables-a-poser/#findComment-1008775 Share on other sites More sharing options...
pandyboy Posted February 8, 2010 Author Share Posted February 8, 2010 It is in a class - I thought about globals, but as the variables are going to be so different each time the showPage function is called it wouldn't work. Sometimes, for example there'll be 2 or 3 MySQL recordsets, sometimes arrays, POST variables, GET variables etc etc. (The last 2 I can deal with of course). Quote Link to comment https://forums.phpfreaks.com/topic/191337-functions-includes-and-variables-a-poser/#findComment-1008783 Share on other sites More sharing options...
ignace Posted February 8, 2010 Share Posted February 8, 2010 Your code mixes presentation- (html) with bussiness logic (php) First off your page1.php should be wrapped by a view: Your page1.php <!DOCTYPE ..> <html> <head> .. <title><?php print $this->title; ?></title> .. </head> <body> .. <div id="content"><?php print $this->content; ?></div> .. </body> </html> The wrapping view can be something like: class MyView { private $variables = array(); public function __set($offset, $value) { .. } public function __get($offset) { .. } public function __isset($offset) { .. } } $view = new MyView(); $view->variable = 'dog'; $view->array1 = array('cat', 'pig', 'gnu'); $view->render('page1.php'); page1.php now contains header and footer but you can put that in an include in the page1.php or use the Two-Step View pattern of Martin Fowler (http://martinfowler.com/eaaCatalog/twoStepView.html) If you don't want to write your own view you can use Smarty which is a templating engine (http://www.smarty.net/) Quote Link to comment https://forums.phpfreaks.com/topic/191337-functions-includes-and-variables-a-poser/#findComment-1008845 Share on other sites More sharing options...
KevinM1 Posted February 8, 2010 Share Posted February 8, 2010 If you haven't set it up as a class and you don't want to pass each variable individually between the functions, you might want to look in to using global variables. http://php.net/manual/en/language.variables.scope.php No. Global variables are not the way to go. They never are. Functions and methods have argument lists for a reason. Use them. Quote Link to comment https://forums.phpfreaks.com/topic/191337-functions-includes-and-variables-a-poser/#findComment-1008901 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.