<?php
class klasa {
public function funkcja($x = 'default', $y = 'value')
{
var_dump($x);
var_dump($y);
}
}
$x = new klasa;
$x->funkcja('other value');
As on the picture, how to omit first parameter which has his default value and only affect second one?
I tried $x->funkcja(NULL, 'other value');
doesn't work, $x->funkcja(, 'other value');
doesn't work too.
How to achieve it?
One of the possible answers. Basically, check if value is null and then assign a default value.
class klasa {
public function funkcja($x, $y = 'value')
{
$x = ($x === null ? 'default' : $x);
var_dump($x);
var_dump($y);
}
}
$x = new klasa;
$x->funkcja(null, 'value');
$x === null
- Check if $x
is null?
'default'
- If it's true assign a default value:
$x
- If it's not true reassign it's value