PHP Classes

File: namedparams.examples.php

Recommend this page to a friend!
  Classes of Stefan Jibrail Froelich   Named Parameters   namedparams.examples.php   Download  
File: namedparams.examples.php
Role: Example script
Content type: text/plain
Description: examples and tutorial
Class: Named Parameters
Call functions passing parameters by name
Author: By
Last change:
Date: 12 years ago
Size: 3,249 bytes
 

Contents

Class file image Download
<pre>
    This class allows you to emulate calling functions with named
    parameters such as in python.
    This is useful in situations where a function or method has many optional parameters
    and you need to specify only a few. Or one that is at the end of the list.
    It uses reflection to do the calls for you.
   
    In all fairness, this is mostly great for functions but a method version has been added
    for the sake of it but might be cumbersome in most situations.
   
    For functions,
        <code>
            NamedParams::Func($function_name, [$arg1], [$arg2], [...]);
        </code>

    For methods,
        <code>
            NamedParams::Method($instance_of_class, $method_name [,$arg1] [,$arg2] [,...]);</code>
       
    The arguments list is supplied as a string.
        eg. <code>
            function Test($one=1, $two=2, $three=3){}
            </code>
        The argument string for $one will be like: '$one=One' OR 'one=One' OR '$one =One'
        The parameter name is on the left and the value on the right
        Parameter names are case sensitive.
        Anything after the '=' is taken as the value, so blank spaces are recognized
            eg. <code>
                '$one= One' != '$one=One'
                </code>
            However, only characters before the '=' are recognized.
            Spaces and leading '$' signs are stripped
       
        Use Case:
            If we wish to call the function while supplying only the third parameter,
            conventionally we will do something like this
            <code>
                test(1, 2, 567);
            </code>
            But with Named parameters, we simply call
            <code>
                NamedParams::func('test', '$three=567');
            </code>
               
        This is the same mostly for calling methods
           
           Function Specific:
            With functions, we can instantiate the class and call the
            given function as a method.
            eg.
            <code>
                $np = new NamedParams;
                $np->test('two=change this');
            </code>
            This method is more compact and does simplifies the calling a lot.
            However, it does not work for methods, only functions
           
            <hr> <hr>
<?php


include 'namedparams.class.php';



class
TestClass
{
    function
testMethod($p1, $p2 = 2, $p3 = 3, $p4 = 4, $p5 = 5, $p6 = 6){
       
var_dump(func_get_args());
    }
}

function
testFunction($p1, $p2 = 2, $p3 = 3, $p4 = 4, $p5 = 5, $p6 = 6){
   
var_dump(func_get_args());
}



echo
'Call with only the required parameter ,$p1 <br><br>', 'function <br>';
NamedParams::func('testFunction', '$p1 =required');

echo
'method <br>';
NamedParams::method(new TestClass(), 'testMethod', '$p1 =required');
echo
'<hr>';

echo
'Call an optional parameter <br><br>',
    
'function <br>';
NamedParams::func('testFunction', '$p1 =required', '$p5 =p5');

echo
'method <br>';
NamedParams::method(new TestClass(), 'testMethod', '$p1 =required', '$p4 =p4');
echo
'<hr>';

echo
'Mix it up a bit <br><br>',
    
'function <br>';
NamedParams::func('testFunction', '$p1 =required', '$p5 =p5', '$p2 =p2', '$p4 =p4');

echo
'method <br>';
NamedParams::method(new TestClass(), 'testMethod', '$p1 =required', '$p4 =p4', '$p2 =p2', '$p6 =p6');
echo
'<hr>';