我是新來的編碼,我正在學習MVC。
你能解釋一下這個簡單的代碼嗎?
model.php
<?php
class Model
{
public $string;
public function __construct()
{
$this->string = "MVC + PHP = Awesome!";
}
}
View.php
<?php
class View
{
private $model;
private $controller;
public function __construct($controller,$model)
{
$this->controller = $controller;
$this->model = $model;
}
public function output()
{
return "<p>" . $this->model->string . "</p>";
}
}
Controller.php這樣
<?php
class Controller
{
private $model;
public function __construct($model)
{
$this->model = $model;
}
}
意見只包含HTML代碼在90%以上,剩下的只是你從你的控制器有變數這是使用方法時在引擎蓋下進行的引導。而你的模型,它可以由多個東西組成,通常分成多個子模型,如數據庫或服務提供者等等。 –
'$ this-> model'是對象屬性(在上面聲明),並且可以被任何對象的方法訪問(像全局變量在函數內部可見而不用作爲參數傳遞它們)。 '$ model'只是傳遞給方法的局部變量(這裏是:構造函數)。方法/函數範圍變量在離開函數後被銷燬,但屬性被保留。 – shudder
標題中的問題(*「$ this-> model = $ model是什麼意思?」*)與MVC無關。這是關於PHP面向對象的編程。請閱讀[documentation](http://php.net/manual/en/language.oop5.php)或查找OOP教程。 – axiac