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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:30:36+00:00 2026-06-05T20:30:36+00:00

I have a custom MVC PHP framework that has a router class, which calls

  • 0

I have a custom MVC PHP framework that has a router class, which calls a controller, which uses a model, then the controller presents the view, etc etc.

My problem is that I can’t figure out technically how to allow variables to pass between the controller and the view, semantically. I could do a quick-and-dirty fix, but what I want to have is this for a controller:

class IndexController extends Controller{
    var $name = "John"; // instance variable
}

And have this for a view:

<p> <?=$name?> </p>

My question is this:

  1. How can I create a Controller->render() function, or something similar, that allows the view to access instance variables from the controller? and,
  2. How can I do this without doing klutzy things like $data['view']['name'] = "John"; or having to write ten lines of code by default for any new controller I make. I want to do this so it’s as DRY as possible.

Thanks.

Edit: FabioCosta’s solution

I’m not sure I understand, so far I have my base controller like this:

<?php
    class Controller{
        public function __get($key){
            if(isset($this->$$key)) return $this->$$key;
        }
    }
?>

My base view class looks like this:

<?php
    class View{
         public $controller;
         public function render(){
         $this->controller = $this;
    }
?>

And I initialize from the router like this:

<?php
    $controller = new IndexController();
    $view = new IndexView();
    $view->render();
?>

However, this doesn’t work, and I know I’m doing something wrong.

  • 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-05T20:30:37+00:00Added an answer on June 5, 2026 at 8:30 pm

    Why not pass the controller that instantiates the view and use the __get magic method?

    like so:

      public function __get($key){
    
          if(isset($this->$key)) return $this->$key;
      }
    

    Here is a working example View.php:

    class View{
       protected $_controller;
       public function __construct(Controller $controller){
            $this->_controller=$controller;
       }
       public function render(){
            echo '<h1>Hello '.$this->_controller->name.'</h1>';
       }
    }
    

    Controller.php

    class Controller{
        protected $name='fabio';
        protected $_myView;
    
        public function __get($key){
    
            if(isset($this->$key)) return $this->$key;
       }
        public function __construct(){
            $this->_myView=new View($this);
        }
    
        public function indexAction(){
            $this->_myView->render();
        }
    }
    

    And the router:

    $c=new Controller();
    $c->indexAction();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom-built MVC PHP framework that I am in the process of
I'm using the MVC 2 RC. I have a custom model binder that used
I'm using spring MVC, and I have a custom authentication/security system that I had
I have just created a mid-sized web-application using Java, a custom MVC framework, javascript.
I am building a custom MVC framework using PHP. My problem is when I
I have a custom error page in my MVC application that's just ~/error/ but
I have a custom MVC application/framework where each action is a function inside of
I have a custom asp.net mvc class validation attribute. My question is how can
I am developing using TDD and ASP.Net MVC. I have a custom ActionFilterAttribute which
I am working with a custom MVC PHP framework and the index page (acting

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.