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

  • SEARCH
  • Home
  • 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 6888155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:58:29+00:00 2026-05-27T05:58:29+00:00

I have the following class: abstract class TheView { public $template = NULL; public

  • 0

I have the following class:

abstract class TheView
{
  public $template = NULL;
  public $variables = array();

  public function set($name, $value)
  {
    $this->variables[$name] = $value;
  }
  public function display()
  {
    include($this->template);
  }
}

The template file is a simple PHP file:

<?php
echo $Message;
?>

How can I make all the variables in TheView::$variables available in the template (the key of each item should be the name of the variable).

I’ve already tried to add all variables to $GLOBALS but that didn’t work (and I think it’s a bad idea).

  • 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-05-27T05:58:30+00:00Added an answer on May 27, 2026 at 5:58 am

    I always end up doing this:

    public function render($path, Array $data = array()){
        return call_user_func(function() use($data){
            extract($data, EXTR_SKIP);
            ob_start();
            include func_get_arg(0);
            return ob_get_clean();
        }, $path);
    }
    

    Note the anonymous function and func_get_arg() call; I use them to prevent $this and other variable “pollution” from being passed into the template. You can unset $data before the inclusion too.

    If you want $this available though, just extract() and include() directly from the method.

    So you can:

    $data = array('message' => 'hello world');
    $html = $view->render('path/to/view.php', $data);
    

    With path/to/view.php:

    <html>
        <head></head>
        <body>
            <p><?php echo $message; ?></p>
        </body>
    </html>
    

    If you want the View object passed, but not from the scope of the render() method, alter it as follows:

    public function render($path, Array $data = array()){
        return call_user_func(function($view) use($data){
            extract($data, EXTR_SKIP);
            ob_start();
            include func_get_arg(1);
            return ob_get_clean();
        }, $this, $path);
    }
    

    $view will be the instance of the View object. It will be available in the template, but will expose only public members, as it is from outside the scope of the render() method (preserving encapsulation of private/protected members)

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

Sidebar

Related Questions

I have the following class: public abstract class AbstractParent { static String method() {
Let's say I have defined the following class: public abstract class Event { public
Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
I have the following Model pattern: public abstract class PARENTCLASS {...} public class CHILD_A_CLASS
I have the following classes in my Model: public abstract class Entity : IEntity
I'm looking at some Java classes that have the following form: public abstract class
I have the following abstract Django models: class Food(models.Model): name = models.CharField(max_length=100) class Meta:
I have the following abstract class which CAN NOT be changed. public abstract class
I have following code: public abstract class TestProperty { public abstract Object PropertyValue {
I have the following class hierarchy [BsonKnownTypes(typeof(MoveCommand))] public abstract class Command : ICommand {

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.