Last active
October 14, 2019 14:53
-
-
Save miltonparedes/35132721753bafa156f94ee83c8e3695 to your computer and use it in GitHub Desktop.
MVC Init
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Controller { | |
private $model; | |
public function __construct(Model $model) { | |
$this->model = $model; | |
} | |
public function textClicked() { | |
$this->model->text = 'Text Updated'; | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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