19

I am getting the error "Undefined variable: interval in C:\wamp\www\DGC\classes\DateFilter.php"

Here is my code for the DateFilter class:

class DateFilter extends Filter
{
    //@param daysOld: how many days can be passed to be included in filter
    //Ex. If daysOld = 7, everything that is less than a week old is included
    private $interval;

    public function DateFilter($daysOld)
    {
        echo 'days old' . $daysOld .'</ br>';
        $interval = new DateInterval('P'.$daysOld.'D');
    }


    function test()
    {
        echo $interval->format("%d days old </br>");
        //echo 'bla';
    }

}

When I create a new instance of the DateFilter class and call test() it give me the error. I realize it means the variable hasn't been initialized, but I know that the constructor is being called because I put an echo statement in there and it was output.

I have also tried: $this::$interval->format(...); self::$interval->format(...); but it didn't work.

I know this is probably an easy fix, sorry for the noob question. Can't believe this stumped me.

4 Answers 4

44

You have to use $this->interval to access the member variable interval in PHP. See PHP: The Basics

class DateFilter extends Filter
{
    private $interval;    // this is correct.

    public function DateFilter($daysOld)
    {
        $this->interval = new DateInterval('P'.$daysOld.'D');   // fix this
    }

    function test()
    {
        echo $this->interval->format("%d days old </br>");     // and fix this
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

echo $this->$interval->format("%d days old </br>"); Is also giving me this same error :/
remove the second dollar sign. It's $this->interval
I've always find quite "strange" that PHP forces you to use $this to access object fields. Many other languages such as C# will not (if the variable has not the same name of a local one). Anyway the use of this is always a good practice, IMHO: it helps you to quick look at object properties and avoid confusion with local variables.
Definitely. Python has the same requirement (with 'self.' although 'self' is just a convention, and you have to actually provide a 'self' parameter in a function). I don't mind, because it makes sure you know what you're actually referencing. Quite important in these dynamic languages when you can declare stuff all over the place.
Thank you for the help! I am use to languages where the this is optional. Anyways, when I take away the 2nd dollar sign, I get this error: Fatal error: Call to a member function format() on a non-object
|
4

$interval is local to the function. $this->interval references your private property.

class DateFilter extends Filter
{
    //@param daysOld: how many days can be passed to be included in filter
    //Ex. If daysOld = 7, everything that is less than a week old is included
    private $interval;

    public function DateFilter($daysOld)
    {
        echo 'days old' . $daysOld .'</ br>';
        $this->interval = new DateInterval('P'.$daysOld.'D');
    }


    function test()
    {
        echo $this->interval->format("%d days old </br>");
        //echo 'bla';
    }

}

1 Comment

Perfect, that was the last piece of the puzzle I was missing.
2
function test()
{
    echo $this->interval->format("%d days old </br>");
}

Comments

0

trying

public var $interval;

and

echo $this->interval;

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.