Jump to content

Functions, Includes and Variables - A Poser


pandyboy

Recommended Posts

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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/)

Link to comment
Share on other sites

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.

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.