0

I am making a class to check for holidays. I made functions for specific holidays and an overarching function to see if a date is a holiday, not one in particular.

I am getting the following error:

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

Here is my code:

<?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;
    }
}
?>

It's throwing the error on the first else statement in isHoliday(). If that's not the proper structure then how should I do it?

1
  • 1
    PHP thinks your else if statements have no bodies - and they don't. For clarify (and avoiding syntactic pitfalls) you should always use braces with if statements. Commented Apr 6, 2014 at 23:46

4 Answers 4

3

You are trying to do a logical or, which can be represented using ||

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);
}

You can also remove the if/else, and simply return the result of the boolean expression itself.

Edit - There are additional errors with your posted code -

Constructor should use $this->date, not $this->$date

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

Further to the above, if you ever need the current instantiated value of $date, then you should use the notation

 $this->date

The function easter_date does not exist

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

After you have fixed this issues, you should be able to use it as -

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

As your functions all seem to be helpers, it might be suggested to look at static functions in php instead :)

Sign up to request clarification or add additional context in comments.

6 Comments

This is probably a good tip but it should be a comment not an answer.
Got this error: atal error: Call to undefined function isNewYears() in C:\xampp\htdocs\mgmt\classes\Holidays.php on line 51
Apologies, forgot the prefix of $this-> to function calls
I have added some additional comments about further issues I have noticed, hopefully it helps :)
Thanks. Can you elaborate on why they should be static? I am really unfamiliar with them. I read a couple of articles but am still not really getting it.
|
1

You kind of need a statement to execute when the if passes...

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

3 Comments

I don't believe this is a suitable answer, as there is nothing useful that could be done within the expression blocks other than 'return true'
Yours is the correct answer, @AlanFoster. I will mark it as such when StackOverflow allows.
The answer is "half correct" -- the solution is not to add something to be executed there... but to add a semicolon after the if which means nothing needs to be executed.
1

EDIT: Disregard my answer. Alan Foster's is better and should be the accepted answer IMO. Try:

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;
    }
}

2 Comments

could you elaborate on $this->$date for me? I was under the impression this meant the variable passed in. It seems as though in PHP it refers to the class member variable?
I am going to remove my answer as AlanFoster's is actually correct, since the functions return booleans already. $this->date is an alias of $date, but only available inside the function unless you call it at the top of the class, like public $date = NULL; would let you pass the same date to any function(as $this->date)
1

This fixed the error. If you don't need any code, leave it like this, otherwise in specific sections you can write your code:

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.