Can I set up a default method argument with class property in PHP? -
i'm using php 5.2.6. want have default value argument in method, seems i'm getting bit clever.
the class property blnoverwrite
defaulted , settable elsewhere in class. have method want have settable again, not override existing value. error when try this:
public function place( $path, $overwrite = $this->blnoverwrite ) { ... }
must this?
public function place( $path, $overwrite = null ) { if ( ! is_null($overwrite) ) { $this->blnoverwrite = $overwrite; } ... }
yes, have way. cannot use member value default argument value.
from php manual on function arguments: (emphasis mine)
a function may define c++-style default values scalar arguments. […] php allows use of arrays , special type null default values. […] the default value must constant expression, not (for example) variable, class member or function call. […] note when using default arguments, defaults should on right side of non-default arguments; otherwise, things not work expected.
Comments
Post a Comment