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 9218661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:54:56+00:00 2026-06-18T02:54:56+00:00

I have a class which contains a method to load a view. This works

  • 0

I have a class which contains a method to load a view. This works perfect and the page is loaded correctly! The problem I’m having is that I can’t seem to figure out a way to pass variables to the view that my method currently tries to load. How can I go by passing variables to the page? Is there a way I can manipulate my current code to make this work? Or am I going in the complete opposite direction?

Controller Class:

<?php
    class Controller {

        private $args = array();

        public function view($page, $args = null) {

            if ($args !== null) {
                $this->args = $args;
                extract($this->args);
            }

            $page = VIEWS . $page . '.php';
            $template = $this->getTemplate($page);

            echo $template;
        }

        private function getTemplate($file) {
            ob_start();
            include($file);
            $template = ob_get_contents();
            ob_end_clean();
            return $template;
        }
    }
    ?>

Controller:

<?php
    class Page extends Controller {

        public function person($age) {
            $data['age'] = $age;
            $this->view('person', $data);
        }

    }
?>

View Contents:

</h1>My name is Dennis. My age is: <?php echo $age; ?></h1>

The end result is an undefined variable ($age).

  • 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-18T02:54:57+00:00Added an answer on June 18, 2026 at 2:54 am

    The variables which you extract in view() are not available in getTemplate(). It is a different scope. You should instead extract them in the same method that does the rendering.

    Also, what you have there is not an MVC view. It’s just a template. A view in MVC is an instance which is responsible for UI logic. It should be requesting the information, that it needs, from model layer and then, if a response is not simply a HTTP location header, creating the HTML using multiple templates.

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

Sidebar

Related Questions

I have an abstract class which contains method for setting header text. It looks
I have class like this below shown. which contains the shopping items where the
Hi i have a class which contains different methods for checking database values. Each
I have a class which contains a static collection that can be used across
I have a class which contains 5 properties. If any value is assingned to
I have a parent class which contains a child object. I am using set
Alright so here is the question. I have a user class which contains a
I have a class library project which contains some content files configured with the
In my console application have an abstract Factory class Listener which contains code for
I have an Entity class, which contains 3 pointers: m_rigidBody, m_entity, and m_parent. Somewhere

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.