[英]How to make a model-view-controller?
I need to modify my code so that it becomes a model-view-controller.我需要修改我的代码,使其变为 model-view-controller。 Since I'm a total noob when it comes to programming my head aches if I'm honest.
因为如果我说实话,在编程方面我完全是个菜鸟。 Any help how to do this would be appreciated.
任何帮助如何做到这一点将不胜感激。
For a better understanding I will paste the whole task so you can see what my code is for.为了更好地理解,我将粘贴整个任务,以便您查看我的代码的用途。
*In one pasture there are 200 bustling sheep. *一个牧场里有200只熙熙攘攘的羊。 The flock consists of 95 white sheep, 60 black sheep and 45 white-black sheep.
羊群由95只白羊、60只黑羊和45只白黑羊组成。 The shepherd now wants to divide them up so that the corresponding wool can be sheared by color.
牧羊人现在想把它们分开,以便可以按颜色剪出相应的羊毛。 Help him write a small script so that he can better order his sheep.
帮他写一个小脚本,这样他就可以更好地命令他的羊了。 Please implement the first three points with variables, arrays, mathematical operators and functions.
请用变量、arrays、数学运算符和函数实现前三点。 Tip: For a better overview, you can also define your own functions.
提示:为了获得更好的概览,您还可以定义自己的函数。
Here's my code sample, as I said the following part has to become a model-view-controller.这是我的代码示例,正如我所说,以下部分必须成为 model-view-controller。 How do i do this?
我该怎么做呢?
Here is my code:这是我的代码:
<?php
$stableForEachColor = array("stableForWhite", "stableForBlack", "stableForWhiteBlack");
// Function to get the full amount of all sheep
function legs($all)
{
$legs = 4;
return $all * $legs;
}
$whiteSheep = 95;
$blackSheep = 60;
$whiteBlackSheep = 45;
$all = $whiteSheep + $blackSheep + $whiteBlackSheep;
echo "At the pasture there are $all sheep in total.\n\n";
$legs = legs($all);
echo "All sheep combined have $legs legs in total.\n\n";
// Names of the white sheep
for ($colorWhite = 1; $colorWhite <= $whiteSheep; $colorWhite++) {
echo "Wooly $colorWhite is white.\n";
}
// Names of the black sheep
for ($colorBlack = 1; $colorBlack <= $blackSheep; $colorBlack++) {
echo "Wooly $colorBlack is black.\n";
}
// Names of the white-black sheep
for ($colorWhiteBlack = 1; $colorWhiteBlack <= $whiteBlackSheep; $colorWhiteBlack++) {
echo "Wooly $colorWhiteBlack is white-black.\n";
}
$stable = array('white_sheep' => $colorWhite, 'black_sheep' => $colorBlack, 'white-black_sheep' => $colorWhiteBlack);
echo '<pre>';
print_r($stable);
$sheep = 200;
$stablesC = 4;
for ($i = $sheep; $i > 0; $i--) {
$stables[rand(0, ($stablesC - 1))] += 1;
}
echo '<pre>';
print_r($stables);
Most of that task involves just restructuring your code, to separate...well, the M, V and C bits.大多数任务只涉及重构代码,以分离......嗯,M、V 和 C 位。
There is no one true answer, but based on a quick glance i suspect the model part may look roughly like:没有一个真正的答案,但基于快速浏览我怀疑 model 部分可能看起来大致如下:
and your view layer can likely be very slim (just a couple methods in total).并且您的视图层可能非常纤薄(总共只有几种方法)。
anyway, maybe this gives you a little of an idea on where to start.无论如何,也许这会让您对从哪里开始有所了解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.