Using class variable as default

Just recently noticed this gives an error…


class foo
{
  private $bar;

  function bah($duh = $this->bar){}
}

Am I doing something wrong or that’s how PHP is designed? In either case, how should I correct this to allow such logic?

It’s by design, and not just class variables, any variables.


class foo
{
    private $bar;

    function bah($duh = ''){
        if(empty($duh)) { $duh = $this->bar; }
    }
}

Ahhh, that’s true, I’ve never really used variable on it either. Thanks!

And if you want the parameter/argument to be the ‘foo’ type then you can do:


class foo
{
  private $bar;

  function bah(foo $duh){}
}

I know this is not required but it can be used like this.