如果在PHP类中有else语句语法问题

时间:2014-04-06 23:42:21

标签: php class if-statement

我正在上课来检查假期。我为特定的假期和一个总体功能创建了功能,以查看日期是否是假日,而不是特别假日。

我收到以下错误:

Parse error: syntax error, unexpected 'else' (T_ELSE) in C:\xampp\htdocs\mgmt\classes\Holidays.php on line 52

这是我的代码:

<?php
class Holidays {

    //private member variables
    private $date;

    //constructors
    public function Holidays() {
        $this->$date = date("Y-m-d");
    }

    //setters
    public function setDate($date) {
        $this->$date = $date;
    }

    //getters
    public function getDate() {
        return $this->$date;
    }

    //member public functions
    public function isNewYears($date) {
        return ($date == date('Y', $date)."-01-01" ? true : false);
    }
    public function isMLKDay($date) {
        return ($date == date("Y-m-d", strtotime("third Monday of January ".date('Y', $date)) ? true : false));
    }
    public function isValentinesDay($date) {
        return ($date == date('Y', $date)."-02-14" ? true : false);
    }
    public function isPresidentsDay($date) {
        return ($date == date("Y-m-d", strtotime("third Monday of February ".date('Y', $date)) ? true : false));
    }
    public function isEaster($date) {
        return ($date == date("Y-m-d", easter_date($date)) ? true : false);
    }
    public function isMemorialDay($date) {
        return ($date == date("Y-m-d", strtotime("last Monday of May ".date('Y', $date)) ? true : false));
    }
    public function isLaborDay($date) {
        return ($date == date("Y-m-d", strtotime("first Monday of September ".date('Y', $date)) ? true : false));
    }
    public function isThanksgiving($date) {
        return ($date == date("Y-m-d", strtotime("fourth Thursday in November".date('Y', $date)) ? true : false));
    }
    public function isChristmas($date) {
        return ($date == date('Y', $date)."-12-25" ? true : false);
    }   
    public function isHoliday($date) {
        if (isNewYears($date))
        else if (isMLKDay($date))
        else if (isValentinesDay($date))
        else if (isPresidentsDay($date))
        else if (isEaster($date))
        else if (isMemorialDay($date))
        else if (isLaborDay($date))
        else if (isThanksgiving($date))
        else if (isChristmas($date))
        else return false;
    }
}
?>

它将错误抛给了isHoliday()中的第一个else语句。如果那不是正确的结构那么我应该怎么做呢?

4 个答案:

答案 0 :(得分:3)

您正在尝试使用||

来表示逻辑或
public function isHoliday($date) {
    return $this->isNewYears($date)
        || $this->isMLKDay($date)
        || $this->isValentinesDay($date)
        || $this->isPresidentsDay($date)
        || $this->isEaster($date)
        || $this->isMemorialDay($date)
        || $this->isLaborDay($date)
        || $this->isThanksgiving($date)
        || $this->isChristmas($date);
}

您也可以删除if / else,并简单地返回布尔表达式本身的结果。

编辑 - 您发布的代码还有其他错误 -

构造函数应使用$ this-&gt; date,而不是$ this-&gt; $ date

public function Holidays() {
    $this->date = date("Y-m-d");
}

除此之外,如果您需要$ date的当前实例化值,那么您应该使用符号

 $this->date

函数easter_date不存在

public function isEaster($date) {
    return ($date == date("Y-m-d", easter_date($date)) ? true : false);
}

修复此问题后,您应该能够将其用作 -

$holidays = new Holidays();
$isHoliday = $holidays->isHoliday(date("Y-m-d"));
if($isHoliday) {
  echo "Today is a holiday!";
} else {
  echo "Today is not a holiday! :(";
}

由于你的函数似乎都是帮手,所以建议你在php中查看静态函数:)

答案 1 :(得分:1)

if通过时,你需要一个声明来执行......

if( isNewYears($date)) do_something_here();
else...

答案 2 :(得分:1)

编辑:忽略我的回答。艾伦福斯特更好,应该是IMO接受的答案。 尝试:

public function isHoliday($date) {
    $this->date = $date;
    if (
        $this->date->isNewYears($date)
        ||
        $this->date->isMLKDay($date)
        ||
        $this->date->isValentinesDay($date)
        ||
        $this->date->isPresidentsDay($date)
        ||
        $this->date->isEaster($date)
        ||
        $this->date->isMemorialDay($date)
        ||
        $this->date->isLaborDay($date)
        ||
        $this->date->isThanksgiving($date)
        ||
        $this->date->isChristmas($date)
    ) 
    { 
        return true; 
    }else{
        return false;
    }
}

答案 3 :(得分:1)

这解决了错误。如果您不需要任何代码,请保持这样,否则在特定部分您可以编写代码:

public function isHoliday($date) {
    if (isNewYears($date)) {}
    else if (isMLKDay($date)) {}
    else if (isValentinesDay($date)) {}
    else if (isPresidentsDay($date)) {}
    else if (isEaster($date)) {}
    else if (isMemorialDay($date)) {}
    else if (isLaborDay($date)) {}
    else if (isThanksgiving($date)) {}
    else if (isChristmas($date)) {}
    else return false;
}