Short answer: Yes, PHP does .
(Note that this is not exactly MVP, as described in its original article, but an option for a web page)
The difference between MVC and MVP is that the view is completely passive and does not know about the model layer. In MVC, it is not passive and does not know about the model layer. In the correct MVP , View class (if any) also SHOULD NOT implement the constructor.
A typical MVP example will consist of the following parts:
- Data Access Level (DataMappers, ORM, etc.)
- Business logic (e.g. validation and computation)
- Passive presentation class (it may be a template, but it would be better to stick to the class)
- Host who connects both Model and View
Example how to implement Model-View-Presenter with PHP
<sub> Note. A model in a real scenario is not a class, but an abstraction layer that contains many classes for working with application logic. I would call it "Model" for demonstration purposes. Sub>
class Model { public function getSomeStuff() { return array('foo' => 'bar'); } } class View { public function render($path, array $vars = array()) { ob_start(); extract($vars); require($path); return ob_get_clean(); } } class Presenter { private $model; private $view; public function __construct(Model $model, View $view) { $this->model = $model; $this->view = $view; } public function indexAction() { $data = $this->model->getSomeStuff();
File: template.phtml
<!DOCTYPE html> <html> <head> <title>...</title> </head> <body> <?php foreach($vars as $key => $value): ?> <p><?php echo $key; ?> : <?php echo $value; ?></p> <?php endforeach; ?> </body> </html>
And the following is used:
$model = new Model(); $view = new View(); $presenter = new Presenter($service, $view); echo $presenter->indexAction();
Please note that this is a very simplified example. In a real scenario, any MVP-based application SHOULD also implement such things as: Router, SPL class autoloader.
Yang
source share