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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:27:26+00:00 2026-06-18T12:27:26+00:00

I have a confusion about the structure of MVC pattern. In some places while

  • 0

I have a confusion about the structure of MVC pattern.

In some places while searching on google I found that the model updates all those views which are subscribed to that model. How the model updates the view in MVC pattern?

Can anyone give me a simple and clear idea how this happen by giving an example?

Thanks

  • 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-18T12:27:27+00:00Added an answer on June 18, 2026 at 12:27 pm

    MVC comes in a variety of flavours. It sounds like you may have been reading about the supervising controller pattern in which the view observes on changes to the model.

    I see from your past questions and answers you like php. I’m not sure how common the supervising presenter is in php (I’ve certainly never used it, but am interested to know if others do). It is common in .Net applications (eg winforms) where a model can be databound to a UI control. The view is notified of changes to the model by subscribing to model events.

    Anyhow, because I thought this would be fun to try in php I put together an example:

    <?php
    
    $input = array(2, 3, 4, 5, 6, 7, 8, 9, 10);
    
    $model = new model(1);
    $controller = new controller(
                  $model, new view($model, 0), new view($model, 2)
                  );
    
    $controller->doAction($input);
    
    class model {
      //the model changed event
      public $modelChangedEvent = array();
      private $val;
    
      public function __construct($val) {
         $this->val = $val;
      }
    
      public function setVal($val) {
         $this->val = $val;
         //raise the model changed event because the model state has changed
         $this->raiseModelChangedEvent();
      }
    
      public function getSquaredVal() {
         return pow($this->val, 2);
      }
    
      private function raiseModelChangedEvent() {
         foreach ($this->modelChangedEvent as $handler)
            call_user_func($handler);
      }
    
    }
    
    class view {
    
      private $model;
      private $decimalPlaces;
      private $valueHistory = array();
    
      public function __construct($model, $decimalPlaces) {
        $this->model = $model;
        $this->valueHistory[] = $model->getSquaredVal();
        $this->decimalPlaces = $decimalPlaces;
        //listen to the model changed event and call handler
        $this->model->modelChangedEvent[] = array(
                                 $this,
                                 'modelChangedEventHandler'
                                  );
      }
    
      public function showView() {
        $formatted = array_map(
                     array($this, 'getFormattedValue'), $this->valueHistory
                     );
        echo implode('<br/>', $formatted), '<br/><br/>';
      }
    
      public function modelChangedEventHandler() {
         $this->valueHistory[] = $this->model->getSquaredVal();
      }
    
      private function getFormattedValue($val) {
         return number_format($val, $this->decimalPlaces);
      }
    
    }
    
    class controller {
    
       private $model;
       private $view1;
       private $view2;
    
       public function __construct($model, $view1, $view2) {
         $this->model = $model;
         $this->view1 = $view1;
         $this->view2 = $view2;
       }
    
       public function doAction($input) {
         foreach ($input as $val) $this->model->setVal($val);
         $this->view1->showView();
         $this->view2->showView();
       }
    
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some confusion about when to use the self keyword during ivars assignment,
I have some confusion about generic programming in java: If Manager is subclass of
I have some basic confusion about how transactions and msdtc work together. I have
I have some confusion about my mysql status. mysql> show status like '%key%'; +------------------------+-------+
I have some confusion about the shared_ptr copy constructor. Please consider the following 2
I have some confusion about the life cycle of native code in Android aps.
i have few confusion regarding ORM(Object Relational Mapping) i am thinking about to use
One thing I have continually found very confusing about using an object database like
i have some questions about constructors in ColdFusion : must i use the name
Hello everyone, I have some confusion regarding some Win32 API data types and macros

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.