r/PHPhelp • u/Jutboy • Nov 29 '24
Best way to handle default parameter values when using wrapper class?
I need to make default parameters values in the system class because they might change based on the system being used. I came up with the following approach but it is very verbose. Is there a better way?
class wrapper {
public function example($parameter = null)
{
$this->system->example($parameter);
}
}
class system {
public function example($parameter)
{
if (is_null($parameter)){ $parameter = 'SystemSpecificValue'; }
// perform actions
}
}
3
Upvotes
1
u/Vroomped Nov 29 '24
system is not defined in this?
otherwise the better option is to just call system.example directly if that's what you meant. A function to call a function sounds very spaghetti to me.
1
u/bkdotcom Nov 29 '24
Are you implementing an interface or extending the wrapped class (and therefore need to match method signature)?
If so, you'll need to dupe the method signature / parameters
If not, you can use
call_user_func_arrayandfunc_get_args3
u/colshrapnel Nov 29 '24
I have a hard time getting what is asked here. But probably you can make it as
1
u/edmondifcastle Nov 29 '24
If you need to have different default parameters depending on the environment, you can use a separate class to resolve parameters.
For example, you have a method public function example($parameter = null).
If $parameter is NULL, you use a resolver:
1
u/minn0w Nov 30 '24
Unsure what your intent really is, but just in case:
0
u/Apprehensive_Ebb_346 Nov 29 '24
Try using enums.