How can one handle setting a class variable as a default value for a method parameter in PHP without causing syntax errors?

When trying to set a class variable as a default value for a method parameter in PHP, you may encounter syntax errors because class properties cannot be used directly in default parameter values. To work around this, you can set the default parameter value to null and then use the class property within the method body to assign a value if the parameter is null.

class MyClass {
    public $defaultValue = 'default';

    public function myMethod($param = null) {
        if ($param === null) {
            $param = $this->defaultValue;
        }
        
        // Rest of the method code here
    }
}