Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referring to class variables as default parameters in PHP function

I would like to have a function in my class that has a default parameter so that one can omit the argument if required. I want the default to be a variable stored in my class;

Hi why is this showing error messages in Aptana?

class property{
    private $id;
    function load_data($id = $this->id){
        //...blah blah blah
    }
}

Should I instead use

class property{
    static $id;
    function load_data($id = self::id){
        //...blah blah blah
    }
}

?

Thanks for the help

like image 502
JackMahoney Avatar asked Dec 20 '22 22:12

JackMahoney


1 Answers

I'm pretty sure you can't do what you're looking for. Instead you should simply check to see if the argument has a value, and if it doesn't, assign the default value which is the object property.

class property{
    private $id;
    function load_data($id = null){
        $id = (is_null($id)) ? $this->id : $id;
    }
}
like image 83
John Conde Avatar answered Apr 27 '23 08:04

John Conde