PHP Classes

File: readme.txt

Recommend this page to a friend!
  Classes of zinsou A.A.E.Mo´se  >  Generic PHP Named Parameters Function Call  >  readme.txt  >  Download  
File: readme.txt
Role: Documentation
Content type: text/plain
Description: readme
Class: Generic PHP Named Parameters Function Call
Extract the values of named function parameters
Author: By
Last change:
Date: 3 years ago
Size: 4,292 bytes


Class file image Download
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. 
For more information send a message to info at phpclasses dot org.