PHP Classes

Generic PHP Named Parameters Function Call: Extract the values of named function parameters

Recommend this page to a friend!
  Info   View files Example   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2019-01-11 (6 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 105 This week: 23All time: 9,030 This week: 20Up
Version License PHP version Categories
named-args-helper 1.0.2Custom (specified...5PHP 5, Language
Description Author

This class can be used to extract the values of named function parameters.

Functions that take parameters passed by name can create an object of this class to extract the values of current function

Parameters can be passed in any order as associative arrays and can be either mandatory or optional.

The class supports parameters that are passed literally by value or by reference.

The class provides also a static function to call any existing function passing named parameters defined as associative arrays.

Innovation Award
PHP Programming Innovation award nominee
October 2018
Number 2
Named function parameters constitute a special way of passing parameters to a function in a way that each parameter can be specified in any order.

This class provides means to extract values of named parameters that can be passed literally by value or by reference.

It can also be used to call any global function passing named parameters defined as associative arrays.

Manuel Lemos
  Performance   Level  
Name: zinsou A.A.E.Mo´se is available for providing paid consulting. Contact zinsou A.A.E.Mo´se .
Classes: 48 packages by
Country: Benin Benin
Age: 29
All time rank: 9431 in Benin Benin
Week rank: 13 Up1 in Benin Benin Equal
Innovation award
Innovation award
Nominee: 21x

Winner: 2x

PHP functions can have as many parameters as needed and the order in which they are defined  really matters.Some may be 
mandatory and some optional.It is also important to specify mandatory parameters before any optional parameters.Sometimes 
There are so many arguments that one could forget the exact order to  specify them when trying to use the function.Sometimes 
we are embarrassed while defining the function by the optimal order to choose in order to make easy the future usage of the 
function and specially when it comes to optional arguments order.Sometimes ,when calling a function we need some optional 
arguments but not all and their predefined order doesn't  facilitate at all things. The present package contains a generic 
class which allows to define  named parameters functions.It allows to define function in a way which allows to call both 
optional and mandatory parameters in any order we want. Of course there are already in the PHP Classes repository some 
packages which tried to solve the same problem.However this package use different approach which actually allows to handle 
any type of parameters and not only those which can be specified as string,and it also handle references.It also allows to specify parameters without naming them just as a simple native function 

How  to use :
let's say that you want to define a function  with parameters $first ,$second,$third,$fourth,$fifth
where $first and $fourth are required and the other optional:

Natively you should do something like

function test($first,$fourth ,$second=null,$third=null,$fifth=null){

	return $first+$second+$third+$fourth+$fifth;

with this package you must proceed like this:

function test(NamedArgs $mandatory){
	$required=['first','fourth'];//specify required parameters here
    $default=['first'=>null,'second'=>null,'third'=>null,'fourth'=>null,'fifth'=>null];//define all parameters required and optional with their default values here
	unset($mandatory);//gain space 
	//then you can use your parameters as you did before in your functions
	return $first+$second+$third+$fourth+$fifth;
 and for the call you can use:
 or either 
 test(Args([1,2,3])); //just as native function but you will then need to respect predefined order as done natively
You can also turn normal functions to named args functions this way:
on my PHP version when i do :

echo  new reflectionFunction('preg_match');

i obtain:

Function [  function preg_match ] {

  - Parameters [5] {
    Parameter #0 [  $pattern ]
    Parameter #1 [  $subject ]
    Parameter #2 [  &$subpatterns ]
    Parameter #3 [  $flags ]
    Parameter #4 [  $offset ]

so i can use the following code to specify my args anywhere in the order i want:


or  even more simply by removing the NamedArgs object step:


Note that i have just called statically preg_match and pass as the only one parameter a NamedArgs object or an array.

which print out as you should see when you run the provided example file:

array(1) {
  array(5) {
    string(1) "i"
    string(1) "o"
    string(1) "a"
    string(1) "u"
    string(1) "e"

 As you can see, few lines of code transform any of your functions to something which will boost your productivity because 
 you won't be forced to search for the exact order in which parameters are defined before call your function and this can be really helpful on big project and you can just switch between named args calling and native calling easily.
 See the TestNAmedArgs.php file to see a complete how to define functions with references and without references and how to call them ....
 Keep in mind that although the parameters are required you can specify them in any order for the call...

 report bugs to or use the forum. 
  Files folder image Files  
File Role Description
Accessible without login Plain text file license.txt Lic. license file
Plain text file NamedArgs.class.php Class class source
Accessible without login Plain text file readme.txt Doc. readme
Accessible without login Plain text file testNamedArgs.php Example example script

 Version Control Unique User Downloads Download Rankings  
This week:23
All time:9,030
This week:20Up