wanting to build an include handler

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

wanting to build an include handler

Post by flying_circus »

I would like to build an include handler into the core of my app, yet still have the included variables available in the scope from which it is called.

example:

Code: Select all

<?php
  class Core
  {
    /* ... */
    
    static function _include($path)
    {
      try {
        if(!empty($path) && file_exists($path))
          include($path);
        else
          throw new Exception("Unable to include file at location: {$path}.  File does not exist.");
      } catch(Exception $e) {
        //TODO: Register Error and Log to Syslog
        exit($e->getMessage());
      }
    }
    
    /* ... */
  }
?>
The trouble is, in my view, when I call Core::_include('include/file'); the variables within the included file are in the scope of Core::_include(), not the local view where it was called.

This is tricky where I include a "config" file for a static menu contents, and then include the decorator object.

Has anyone come up with any slick solutions to this problem?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: wanting to build an include handler

Post by Benjamin »

Return the path instead of including the file. e.g.

Code: Select all

include $tpl->getPath();
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: wanting to build an include handler

Post by tr0gd0rr »

If you are wanting to render a template file (e.g. MVC pattern), you could also pass on the vars you want and use compact() and extract(). For example:

Code: Select all

$fname = 'John';
$lname = 'Doe';
$viewVars = compact('fname','lname');
echo Core::renderTemplate($tplPath, $viewVars);
// and in Core class
class Core {
  public static function renderTemplate($tplPath, $viewVars) {
    // check if tpl exists
    extract($viewVars); // now $fname and $lname are available in the template file
    ob_start();
    include($tplPath);
    return ob_get_clean();
  }
}
You can also create a Template class that works similarly but then allows Template instance functions to be available in the view (e.g. $this->func()).
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: wanting to build an include handler

Post by flying_circus »

Thank you both for your insight. I'm not sure why I didnt think of the simple solution that Benjamin posted.

Tr0gd0rr (&#x266b; burninating all the people &#x266b;), thanks for suggesting an alternative approach. I will give it some thought to see if it is a good fit for the current project, or has an application in a future one.

I've been so busy lately with my mind on other vectors, but I did consider your suggestions and wanted to say thanks before too much time went by.

Happy Holidays!
Post Reply