0
0
PHPprogramming~20 mins

MVC architecture overview in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MVC Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this PHP MVC code snippet?

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
<?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();
ANo output
BHello from Model
CFatal error: Call to undefined method
DHello from View
Attempts:
2 left
💡 Hint

Think about which class returns the string and which class prints it.

🧠 Conceptual
intermediate
1:00remaining
Which MVC component is responsible for handling user input?

In the MVC architecture, which component typically handles user input and decides what to do next?

AModel
BView
CDatabase
DController
Attempts:
2 left
💡 Hint

Think about who acts like the traffic cop directing traffic.

🔧 Debug
advanced
2:30remaining
Why does this MVC PHP code cause an error?

Look at this PHP MVC code. Why does it cause a fatal error?

PHP
<?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();
AController constructor missing parameters
BModel class missing getData method
CUndefined variable $data in View::render()
DView class cannot echo strings
Attempts:
2 left
💡 Hint

Check the variable usage inside the View's render method.

📝 Syntax
advanced
1:30remaining
Which option fixes the syntax error in this MVC PHP snippet?

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);
    }
}
AAdd a semicolon after getData() call
BChange getData() to getData;
CRemove parentheses from getData
DAdd curly braces around getData()
Attempts:
2 left
💡 Hint

Look carefully at the end of the line calling getData().

🚀 Application
expert
2:00remaining
How many items are in the array passed from Model to View?

The Model returns an array of users to the Controller, which passes it to the View. How many users will the View receive?

PHP
<?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();
A3
B2
C1
D0
Attempts:
2 left
💡 Hint

Count the number of user arrays inside the returned array.