Skip to content

Instantly share code, notes, and snippets.

@miltonparedes
Last active October 14, 2019 14:53
Show Gist options
  • Save miltonparedes/35132721753bafa156f94ee83c8e3695 to your computer and use it in GitHub Desktop.
Save miltonparedes/35132721753bafa156f94ee83c8e3695 to your computer and use it in GitHub Desktop.
MVC Init
<?php
class Controller {
private $model;
public function __construct(Model $model) {
$this->model = $model;
}
public function textClicked() {
$this->model->text = 'Text Updated';
}
}
?>
<?php
class Model {
public $text;
public function __construct() {
$this->text = 'Hello world!';
}
}
class View {
private $model;
public function __construct(Model $model) {
$this->model = $model;
}
public function output() {
return '<h1>' . $this->model->text .'</h1>';
}
}
class Controller {
private $model;
public function __construct(Model $model) {
$this->model = $model;
}
}
//initiate the triad
$model = new Model();
//It is important that the controller and the view share the model
$controller = new Controller($model);
$view = new View($model);
echo $view->output();
?>
<?php
$model = new Model();
//It is important that the controller and the view share the model
$controller = new Controller($model);
$view = new View($model);
if (isset($_GET['action'])) $controller->{$_GET['action']}();
echo $view->output();
?>
<?php
class View {
private $model;
public function __construct(Model $model) {
$this->model = $model;
}
public function output() {
return '<a href="mvc.php?action=textclicked">' . $this->model->text . '</a>';
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment