Consider a simple MVC setup where the Controller calls the Model to get data and then passes it to the View. What will be printed?
<?php class Model { public function getData() { return 'Hello from Model'; } } class View { public function render($data) { echo $data; } } class Controller { private $model; private $view; public function __construct() { $this->model = new Model(); $this->view = new View(); } public function show() { $data = $this->model->getData(); $this->view->render($data); } } $controller = new Controller(); $controller->show();
Think about which class returns the string and which class prints it.
The Model's getData() method returns the string 'Hello from Model'. The Controller calls this method and passes the result to the View's render() method, which prints it.
In the MVC architecture, which component typically handles user input and decides what to do next?
Think about who acts like the traffic cop directing traffic.
The Controller handles user input, processes it, and decides which Model or View to use next.
Look at this PHP MVC code. Why does it cause a fatal error?
<?php class Model { public function getData() { return 'Data'; } } class View { public function render() { echo $data; } } class Controller { private $model; private $view; public function __construct() { $this->model = new Model(); $this->view = new View(); } public function show() { $data = $this->model->getData(); $this->view->render(); } } $controller = new Controller(); $controller->show();
Check the variable usage inside the View's render method.
The View's render() method tries to echo $data, but $data is not defined inside that method's scope, causing a fatal error.
Find the option that fixes the syntax error in this PHP MVC code:
class Controller {
public function show() {
$data = $this->model->getData()
$this->view->render($data);
}
}Look carefully at the end of the line calling getData().
PHP requires a semicolon at the end of each statement. Missing it causes a syntax error.
The Model returns an array of users to the Controller, which passes it to the View. How many users will the View receive?
<?php class Model { public function getUsers() { return [ ['name' => 'Alice', 'age' => 30], ['name' => 'Bob', 'age' => 25], ['name' => 'Charlie', 'age' => 35] ]; } } class View { public function render($users) { echo count($users); } } class Controller { private $model; private $view; public function __construct() { $this->model = new Model(); $this->view = new View(); } public function showUsers() { $users = $this->model->getUsers(); $this->view->render($users); } } $controller = new Controller(); $controller->showUsers();
Count the number of user arrays inside the returned array.
The Model returns an array with 3 user arrays. The View counts and prints 3.