Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8167139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:06:34+00:00 2026-06-06T20:06:34+00:00

I am trying to build a php class that will let me include other

  • 0

I am trying to build a php class that will let me include other php “template” files to display. I want all of the variables that are in the scope that it is called from to be available in the included file. Here’s the catch, I want to pass off the actual including to a helper method to keep the code DRY. Here is what I have so far:

/* TemplateLoader.php */
class TemplateLoader {
  public function foo() {
    $var = "FOO!";
    //Works
    include 'template.php';
  }

  public function bar() {
    $var = "BAR!";
    //Doesn't work
    $this->render("template");
  }

  private function render( $name ) {
    include $name . '.php';
  }
}

/* template.php */
<?php echo $var; ?>

My question is: How can I accomplish the behaviour of including the template directly in the original method while still using helper method to actually do the “heavy lifting”? I would really appreciate any help you can give!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T20:06:36+00:00Added an answer on June 6, 2026 at 8:06 pm

    This is what first came to my mind – I’m not sure I like it too much, but I’m not sure of a generic alternative. This captures all of the current variables with get_defined_vars(), and passes them to render(), where they are they extract()ed, and thus accessible by the included file.

    You could probably filter the return from get_defined_vars() before you pass it to render(), but I should work.

    public function bar() {
        $var = "BAR!";
        $this->render("template", get_defined_vars());
    }
    
    private function render( $name, &$vars) {
        extract( $vars);
        include $name . '.php';
    }
    

    You could probably filter the return from get_defined_vars() before you pass it to render(), but it should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build an abstract base controller that will extend all other
Below are the files in PHP that I am trying to include a file
I'm trying to build a PHP function that allows me to have an array
I am trying to build a PHP class to check the username and password
I'm trying to build an Activity that will serve as a Calendar Day View.
I'm trying to build a category/subcategory class with php & MySQL. I am stuck
I'm trying to build a front-end and back-end navigation menu. I want to display
I am trying to build an automated email that will send attachments based on
I am trying to build a menu in Wordpress that will look like this
i am trying to build simple php crawler for this purpose i am getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.