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 (11)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 114 This week: 1All time: 9,569 This week: 571Up
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
Picture of zinsou A.A.E.Moïse
  Performance   Level  
Name: zinsou A.A.E.Moïse is available for providing paid consulting. Contact zinsou A.A.E.Moïse .
Classes: 50 packages by
Country: Benin Benin
Age: 34
All time rank: 6781 in Benin Benin
Week rank: 420 Up2 in Benin Benin Down
Innovation award
Innovation award
Nominee: 23x

Winner: 2x

Example

<?php
require_once('./src/namedArgs.php');
require_once(
'./src/namedArgsHelper.php');


function
codeWithoutRef(NamedArgs $mandatory)
{
   
$required = ['first', 'fourth']; //specify required parameters here
   
$default = ['first'=>0, 'second'=>1, 'third'=>2, 'fourth'=>9, 'fifth'=>7]; //define all parameters required and optional with their default values here
   
extract($mandatory->getParams($required, $default));
    unset(
$mandatory);
    return
$first + $second + $third + $fourth + $fifth;
}


function
codeWithRef(NamedArgs $mandatory)
{
   
$required = ['first'];
   
$default = ['first'=>0, 'second'=>1, 'third'=>2];
   
extract($mandatory->getParams($required, $default), EXTR_OVERWRITE | EXTR_REFS);
    unset(
$mandatory);
   
$first = $first + $second + $third;
}


function
test(&$tada, &$tada2, &$test = 6)
{
   
$tada = 1;
   
$tada2 = 2;
    return
$tada + $tada2 + $test;
}


echo
"<pre>";
var_dump(codeWithoutRef(Args(['fourth'=>9, 'first'=>3, 'third'=>79])));
var_dump(codeWithoutRef(Args([1, 2, 3, 0])));


$first = 3;
codeWithRef(Args(['third'=>79, 'first'=>&$first]));
var_dump($first);
$first2 = 3;
codeWithRef(Args([&$first2, 79]));
var_dump($first2);

$tada = $tada2 = null;
var_dump(NamedArgs::test(args(['tada'=>&$tada, 'tada2'=>&$tada2])), $tada, $tada2);

$tada = $tada2 = null;
var_dump(NamedArgs::test(args([&$tada, &$tada2])), $tada, $tada2);


NamedArgs::preg_match_all(args(['subpatterns'=>&$matches, 'pattern'=>'#a|o|i|u|e#', 'subject'=>'gris|gros|gras|grue']));

var_dump($matches);
var_dump($x = NamedArgs::strtoupper(args(['str'=>'gris|gros|gras|grue'])), NamedArgs::strtolower(args(['str'=>$x]))); //just for funny example.
NamedArgs::preg_match(['subpatterns'=>&$match, 'pattern'=>'#a|o|i|u|e#', 'subject'=>'gris|gros|gras|grue']);
var_dump($match);

highlight_string('
<?php
var_dump(codeWithoutRef(Args([\'third\'=>79])));//generate error here for example
?>
'
);
var_dump(codeWithoutRef(Args(['third'=>79])));

echo
"</pre>";


Details

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 extract($mandatory->getParams($required,$default)); 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: test(Args(['fourth'=>9,'first'=>3,'third'=>79])); 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: NamedArgs::preg_match_all(args(['subpatterns'=>&$matches,'pattern'=>'#a|o|i|u|e#','subject'=>'gris|gros|gras|grue'])); or even more simply by removing the NamedArgs object step: NamedArgs::preg_match_all(['subpatterns'=>&$match,'pattern'=>'#a|o|i|u|e#','subject'=>'gris|gros|gras|grue']); 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) { [0]=> array(5) { [0]=> string(1) "i" [1]=> string(1) "o" [2]=> string(1) "a" [3]=> string(1) "u" [4]=> 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 leizmo@gmail.com or use the forum.

  Files folder image Files  
File Role Description
Files folder imagesrc (2 files)
Files folder imagetests (1 file)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file Args.php Example Example script
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file readme.txt Doc. readme
Accessible without login Plain text file testNamedArgs.php Example example script

  Files folder image Files  /  src  
File Role Description
  Plain text file namedArgs.php Class Class source
  Plain text file namedArgsHelper.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file NamedArgsTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 90%
Total:114
This week:1
All time:9,569
This week:571Up