我有以下的代码,每次我提交一个值时,它并没有将它添加到数组中。就好像它创建了一个只包含我提交的值的新数组。
<?php
class Model
{
public $task;
public function __construct()
{
$this->task = array();
}
}
class Controller
{
public $model;
public function __construct(Model $model)
{
$this->model = $model;
}
public function addTask($taskToAdd)
{
array_push( $this->model->task, $taskToAdd);
}
}
class View
{
public $model;
public $controller;
public function __construct(Controller $controller, Model $model)
{
$this->controller = $controller;
$this->model = $model;
}
public function drawScreen()
{
$htmlForm = "<!doctype HTML>";
$htmlForm = $htmlForm . "<html><head><title>php form test</title></head>";
$htmlForm = $htmlForm . "<body>";
$htmlForm = $htmlForm . "<form method='POST' action='index.php'>";
$htmlForm = $htmlForm . "<input type='text' name='newtask' />";
$htmlForm = $htmlForm . "<input type='submit' value='Add new task' />";
$htmlForm = $htmlForm . "</form></body></html>";
echo $htmlForm;
}
public function listTasks()
{
print_r($this->model->task);
}
}
$model = new Model();
$controller = new Controller($model);
$view = new View($controller, $model);
if (isset($_POST['newtask'])) {
$taskToAdd = $_POST['newtask'];
$controller->addTask($taskToAdd);
}
echo $view->drawScreen();
echo $view->listTasks();
我不确定我在这里做错了什么。array_push不是正确的方式吗?我正在使用xampp,如果有区别的话。你有什么想法,我做错了什么?