如何将方法的默认参数设置为类成员

Jak*_*ris 3 php oop class-design class

我在php类中有这个:

$this->ID = $user_id;

public function getUserFiles($id = $this->ID) { } // error here
Run Code Online (Sandbox Code Playgroud)

但显然我不允许以这种方式使用房产.那么我该如何声明方法参数的默认值应该是ID属性的值?

必须有一个比这更好的方法:

$this->ID = $user_id;

public function getUserFiles($id = '') {
    $id = ($id == '') ? $this->ID : $id;
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*sky 10

我通常在这种情况下使用null:

public function getUserFiles($id = null)
{
    if ($id === null) {
        $id = $this->id;
    }
    // ...
}
Run Code Online (Sandbox Code Playgroud)