在面向对象中合并函数


Incorporating functions in oop

我正试图为我的学校作业之一写一个代码,但我找不到它不工作的原因。可能是我还没意识到的简单原因。谁能给我指个正确的方向?我试图使一个类生成3下拉框选择日,月和年。第三个函数似乎也有一个无限循环。代码如下,分为两个文件:

文件1:datselect.php

<?php
    include ("datselect_functions.php");
    $cls = new Dropdown();
    $monthname = array(1 => "January", "February", "March", "April", "May", "June",
                     "July","August", "September", "October", "November", "December");
    $today = time(); 
    $f_today = date("M-d-Y",$today);
    $func = insert_day();
    $func = insert_month($monthname);
    //$func = insert_year($today);
    echo html_head($f_today);
    echo $cls -> getHTML();
    echo html_foot();
?>

文件2:datselect_functions.php

<?php
function html_head($f_today) {
    return "<html>
            <head>
            <title>Select a date</title>
            </head>
            <body>
            <div>
                Today is ".$f_today."
            </div>";
}
class Dropdown {
    private $name = "";
    private $options = array();
    private $selected = "";
    public function __construct($name = "") {
        $this->name=$name;
    }
    public function setOption($var,$label) {
        $this->options[$var]=$label;
    }
    public function getHTML() {
        $html = "<select name='".$this->name."'>";
        var_dump($this->name);
        foreach($this->options as $var => $label) {
            if ($this->selected=$var) {
                $html.= "<option selected value='".$var."'>.$label.</option>";
            } else {
                $html.= "<option value='".$var."'>.$label.</option>";
            }
        }
        $html.= "</select>";
        return $html;
    }
}
function html_foot() {
return "</body></html>";
}
function insert_day(){
    $ddd = new Dropdown("day");
    for($i=1; $i<=31; $i++){
        $ddd -> setOption($i, $i);
    }
}
function insert_month($monthname){
    $ddm = new Dropdown("month");
    foreach($monthname as $key => $value) {
        $ddm -> setOption($key, $value);
    }
}
function insert_year($today){
    $currentyear = date("Y", $today);
    $ddy = new Dropdown("year");
    for($n=$currentyear;$n=$currentyear+3;$n++) {
        $ddy -> setOption($n, $n);
    }
}
?>

有很多地方会出错。

//this is unnecessary, you now create an empty dropdown and echo it here.
$cls = new Dropdown();    
echo $cls -> getHTML();
//instead of this
$func = insert_day();
$func = insert_month($monthname);
//$func = insert_year($today);
//do this
$days = insert_day();
$months = insert_month($monthname);
$years = insert_year($today);
//then make the dropdows appear like this.
echo $days;
echo $months;
echo $years;
//These 3 functions require an extra line of code.
function insert_day(){
    $ddd = new Dropdown("day");
    for($i=1; $i<=31; $i++){
        $ddd -> setOption($i, $i);
    }
    //add this line to all of these three functions, but change the var name
    return $ddd->getHTML();
}

这应该可以解决您的大多数错误,但现在可能会出现一些其他的错误。

你的下拉列表类只是一个生成下拉列表html代码的助手。您需要存储生成的代码或将其输出到某个地方,以便"它工作"。

$func = insert_day(); // 1
$func = insert_month($monthname); // 2
//$func = insert_year($today);
echo html_head($f_today); // 3
echo $cls -> getHTML(); // 4
echo html_foot(); // 5

你正在一步步地做下面的事情:

  1. 你将日期选择的下拉html代码分配给$func
  2. 您将月份选择的下拉html代码分配给$func,覆盖从1到1的所有内容。你应该为它使用一个新变量,或者如果你想要的话,在它后面加上$func .= insert...
  3. 你输出你的标题html
  4. 你输出一个空的下拉菜单,因为下拉菜单实例$cls没有在任何地方配置选项数组
  5. 输出页脚html

缺少的是您还应该输出1的结果。和2。后3。此外,insert_year中的无限循环是因为您在for循环中分配了当前年份+ 3,而不是比较它(===)。

每个类只有一个文件是很好的风格,但这不是这里的问题。

希望对你有帮助。

"="不是比较,而是设置(使用"=="代替):

if ($this->selected=$var) {

死循环:

for($n=$currentyear;$n=$currentyear+3;$n++) {
使用

:

for($n=$currentyear;$n < $currentyear+3;$n++) {

你的insert_day, insert_month函数没有任何用途。下面我已经更新了你的代码来显示天数,虽然你真的不需要这么多代码。

//dateselect.php
<?php
include ("datselect_functions.php");

$cls = new Dropdown();
$monthname = array(1 => "January", "February", "March", "April", "May", "June", "July",
                   "August", "September", "October", "November", "December");
$today = time(); 
$f_today = date("M-d-Y",$today);
$func = $cls->insert_day();
//$func = insert_month($monthname);
//$func = insert_year($today);
echo html_head($f_today);
echo $cls -> getHTML();
//echo html_foot();
?>

//datselect_functions

<?php
function html_head($f_today) {
return "<html>
            <head>
                <title>
                    Select a date
                </title>
            </head>
            <body>
                <div>
                    Today is ".$f_today."
                </div>";
}
class Dropdown {
private $name = "";
private $options = array();
private $selected = "";
public function __construct($name = "") {
    $this->name=$name;
}
public function setOption($var,$label) {
    $this->options[$var]=$label;
}
public function getHTML() {
    $html = "<select>";
    //var_dump($this->options);
    foreach($this->options as $var => $label) {
        if ($this->selected==$var) {
            $html.= "<option selected value='".$var."'>.$label.</option>";
        }
        else {
        $html.= '<option value="'.$var.'">'.$label."</option>";
        }
    }
    $html.= "</select>";
    //print_r($html);
    return $html;
}
function insert_day(){
for($i=1; $i<=31; $i++){
    $this -> setOption($i, $i);
}
}
function html_foot() {
return "</body></html>";
}

}
function insert_month($monthname){
$ddm = new Dropdown("month");
foreach($monthname as $key => $value) {
    $ddm -> setOption($key, $value);
}
}
function insert_year($today){
$currentyear = date("Y", $today);
$ddy = new Dropdown("year");
for($n=$currentyear;$n<=$currentyear+3;$n++) {
    $ddy -> setOption($n, $n);
}
}
?>