Merge func_get_args() with function defaults
<?php
class utils {
/**
* @param mixed[] $args
* @param ReflectionMethod $reflectionMethod
*
* @return array
*/
public static function mergeArgsWithDefaults( $args, \ReflectionMethod $reflectionMethod ) {
foreach ( array_slice( $reflectionMethod->getParameters(), count( $args ) ) as $param ) {
/**
* @var ReflectionParameter $param
*/
$args[] = $param->getDefaultValue();
}
return $args;
}
}
class sampleParent {
const USER_FILE_TYPE_FILE = 'FILE';
public function select( $idUserFile = null, $idUserFileType = self::USER_FILE_TYPE_FILE ) {
echo '[$idUserFile=>' . $idUserFile . ', $idUserFileType=>' . $idUserFileType, ']<br/>' . PHP_EOL;
}
}
class sample extends sampleParent {
const USER_FILE_TYPE_IMG = 'IMG';
public function select( $idUserFile = null, $idUserFileType = self::USER_FILE_TYPE_IMG ) {
return call_user_func_array( 'parent::select', \utils::mergeArgsWithDefaults( func_get_args(), new ReflectionMethod( __CLASS__, __FUNCTION__ ) ) );
}
}
$sample1 = new sampleParent();
$sample1->select();//Prints "" / self::USER_FILE_TYPE_FILE
$sample1->select(1);//Prints 1 / self::USER_FILE_TYPE_FILE
$sample1->select(2, 'test 1');//Prints 2 / "test 1"
echo '<br/>' . PHP_EOL;
$sample2 = new sample();
$sample2->select();//Prints "" / self::USER_FILE_TYPE_IMG
$sample2->select(3);//Prints 3 / self::USER_FILE_TYPE_IMG
$sample2->select(4, 'test 2');//Prints 4 / "test 2"
?>