Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Aaaaarghhh ... uments ;) 2

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR

Hi guys :)

I'm currently modifying a calendar script and I'm having a headache with the arguments that need to be passed to the function. I'd like to ommit any argument I want but the problem is that when I want to use the last argument, I have to either set the previous ones to null or state them.

Example :

Code:
function hd_time_calendar(
$year_to_show, 
$month_to_show, 
$days_to_link = array(), 
$prevnext_to_link = array(),
$day_name_length = 3, 
$cur_month_url = NULL, 
$first_day_of_week = 1,
$setlocal_str_new = "'fra', 'fr_FR'"
) {

// - ! - set localization

    if (is_array($setlocal_str_new)) {
    
    setlocale(LC_TIME, $setlocal_str_new);
    
    } else {
    
    $setlocal_str_new = "setlocale(LC_TIME, $setlocal_str_new);";
    eval($setlocal_str_new);
    
    }

...
}

// now, when I call the function ...

// this works :
hd_time_calendar($cal_year_to_show, $cal_month_to_show, NULL, NULL, 3, NULL, NULL, "'fra', 'fr_FR'");

// this doesn't :
hd_time_calendar($cal_year_to_show, $cal_month_to_show, "'fra', 'fr_FR'");

So, how can I use this function with more freedom in the arguments use?
Only the first two arguments should be mandatory.

Do I have to use classes instead?
If so, what would the code look like once transformed into a class? (never made a class before
and all the tutorials on the subject left me clueless)

The easiest solution would be to declare global variables before the function call
but it doesn't sound like a good idea ...



$_Thanks *= 100000 :)
 
you need to make the latter arguments optional
Code:
function IamAFunction($string, $option1=NULL, $option2=NULL ...){

}

if no value is provided then the NULL default (or whatever it is that you set, is stored).

The alternative is that you pass all the variables in by an array and so something like this

Code:
function IamAFunction($mandatory, $options=NULL){
 $defaults = array(
                  'option1'=>'value1',
                  'option2'=>'value2');
 $defaults = array_merge($defaults, $options);
 //now use the $defaults as usual
 //or use this to bring the variables into ordinary scope
 extract($defaults);
}

this latter method I find very useful in javascript coding and so have imported it into my everyday php toolbox too.
 

Hi jpadie :)

Actually, I've found a way that is more flexible for me.

Code:
function hd_time_calendar($arg) {

    // - ! - available arguments and their default values
    if (!$arg["year_to_show"]) $arg["year_to_show"] = date("Y", time());
    if (!$arg["month_to_show"]) $arg["month_to_show"] = date("m", time());
    if (!$arg["days_to_link"]) $arg["days_to_link"] = array();
    if (!$arg["prne_to_link"]) $arg["prev_to_link"] = array();
    if (!$arg["day_name_strlen"]) $arg["day_name_strlen"] = 3;
    if (!$arg["cur_month_url"]) $arg["cur_month_url"] = "";
    if (!$arg["first_day_of_week"]) $arg["first_day_of_week"] = 1;
    if (!$arg["setlocal_str_new"]) $arg["setlocal_str_new"] = "'fra', 'fr_FR'";


// - ! - set localization
$setlocal_str_old = setlocale(LC_TIME, NULL);

    if (is_array($arg["setlocal_str_new"])) {
    
    setlocale(LC_TIME, $arg["setlocal_str_new"]);
    
    } else {
    
    $arg["setlocal_str_new"] = "setlocale(LC_TIME, " . $arg["setlocal_str_new"] . ");";
    eval($arg["setlocal_str_new"]);
    
    }
    
    ...
    
 }

.................

$arg["year_to_show"] = $year;
$arg["month_to_show"] = $month;
$arg["setlocal_str_new"] = "'fra', 'fr_FR'";

echo hd_time_calendar($arg);

By doing this, I can ommit any argument and set default values for every omitted arguments.
I've spent some time looking at class programming but I'm not ready for that yet :(

But there is something I'm wondering :
Is there a way to shorten
Code:
if (!$arg["year_to_show"]) $arg["year_to_show"] = date("Y", time());
so that $arg["year_to_show"] isn't repeated twice?

BTW, isn't the MVP listing broken???
I mean there is no reason for my username to appear in 3rd position! ;)
 
that's really just the same as my second method by another route.

the way to avoid repetition is by following my coding or something similar. establish the defaults and then override them

Code:
function [  ] ($arg){
$_arg["year_to_show"] = date("Y", time());
extract($_arg);
extract($arg);
//i'd rather use array_merge though
}

and .... no ... MVP list not broken but the rules are arcane. ride the wave while it last! keep posting helpful posts!
 
wow sorry ... I overlooked the most useful part of your code!

Yeah, array_merge is very handy here.

I didn't know that ...
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one.

:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top