Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Private class variables giving error: undefined variable

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.

like image 827
CHawk Avatar asked Oct 12 '11 22:10

CHawk


People also ask

How do I fix undefined variable error in PHP?

Fix Notice: Undefined Variable by using isset() Function This notice occurs when you use any variable in your PHP code, which is not set. Solutions: To fix this type of error, you can define the variable as global and use the isset() function to check if the variable is set or not.

How do you make a variable private in PHP?

Use the __setter (__set) function to set value(s) to your private variable inside a the class, and when the value is needed, use the __getter (__get) function to return the values.

What does undefined mean in PHP?

Notice Undefined Index in PHP is an error which occurs when we try to access the value or variable which does not even exist in reality. Undefined Index is the usual error that comes up when we try to access the variable which does not persist.

How do you declare a class variable in PHP?

The var keyword in PHP is used to declare a property or variable of class which is public by default. The var keyword is same as public when declaring variables or property of a class.


2 Answers

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
    }
}
like image 186
Jonathon Reinhart Avatar answered Sep 28 '22 16:09

Jonathon Reinhart


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

}
like image 26
Herbert Avatar answered Sep 28 '22 16:09

Herbert