I'm a rookie with MVC and OOP and I'm trying to learn it with tutorials and by trying some stuff out myself. I've got this code:
Model.class.php:
class Model {
public $string;
// Add a value to the instance variable $string when object of this class is instantiated
public function __construct(){
$this->string = "MVC + PHP = Awesome!";
}
}
Controller.class.php:
class Controller {
private $model;
// For now, the Controller doesn't do anything, because no controlling features have been implemented yet
public function __construct($model) {
$this->model = $model;
}
}
My View.class.php:
class View {
private $model;
private $controller;
private $account;
public function __construct($controller,$model) {
$this->controller = $controller;
$this->model = $model;
$this->account = $account;
}
// Return the value of the $string variable
public function output(){
return $this->model->string;
}
}
This is my index.php where a simple string is echoed, as a result of the code above:
$model = new Model();
$controller = new Controller($model);
$view = new View($controller, $model);
$account = new Account();
// Execute the output() function from the View class to output the text
echo $view->output();
I've created the account class myself and it should output 'Yoo' instead of 'PHP + MVC = Awesome!' which looks like this:
class Account extends Model {
// Add a value to the instance variable $string when object of this class is instantiated
public function __construct(){
$this->string = "Yoo";
}
}
When I do return $this->model->string; it correctly outputs PHP + MVC = Awesome!. But when I do return $this->account->string, it outputs nothing. No errors or anything. Why isn't this working?
View, you have$this->account = $account, but$accountis undefined in that function.