Jump to content

Function with different number of arguments


Mardoxx

Recommended Posts

I got the idea when helping here: http://www.phpfreaks.com/forums/index.php/topic,267209.0.html

 

I've created this very sketchy function for pushing string variables into a function:

(hopefully you can see how it works)

<?php
function arg_splitter($input,$arg_count=false,$args_num='args_num') {
$split = preg_replace('/\$|\"/','', preg_split('/\"[\s,]+\$/',$input));
/*  First this splits the inputted string on any number of commas
 *	or spaces inbetween the characters " and $
 *  then it replaces all $ and " characters with nothing
 *	my regex skillz suck.
 */
foreach ($split as $key=>$val) {
	$split[$key] = explode("=",$val);	
	$assocarray[$split[$key][0]] = $split[$key][1]; //creates associative array so you can use extract on it
}
if($arg_count) {
	$assocarray[$args_num]=count($assocarray);
}
return $assocarray;
}


function newfn($inputs) {

extract(arg_splitter($inputs,true,'arg_count')); // Only line needed to be added

if(isset($override) && !is_null($override)) {
	echo "O MAYN, WE GOT OVERRIDDEN!!!!\n<br />";
	echo "\$override = $override\n<br />";
	return;
}
echo "\$a = $a\n<br />";
echo "\$b = $b\n<br />";
echo "\$c = $c\n<br />";
echo "\$d = $d\n<br />";
echo "\$arg_count = $arg_count\n<br /><br />";
}

echo "Test 1: \n<br />";
newfn('$a="apple",$b="banana", $c="carrot",$d="date"');
echo "Test 2: \n<br />";
newfn('$override="apple, banana, carrot, duck"');
?>

 

gives an output of:

 

Test 1:
$a = apple
$b = banana
$c = carrot
$d = date
$arg_count = 4

Test 2:
O MAYN, WE GOT OVERRIDDEN!!!!
$override = apple, banana, carrot, duck 

 

 

Any easier/other way of doing this?

 

 

thanks

Link to comment
Share on other sites

Why not just pass the data as an array to begin with, wouldn't that just be easier?

 

function newfn(array $data) {
    foreach($data as $key => $value) {
        // do whatever...
    }
    // whatever here
}

newfn(array('a'=>'apple', 'b'=>'banana', 'c'=>'carrot', 'd'=>'date'));

Link to comment
Share on other sites

can extract handle arrays and stuff?

 

What do you think extract() is for?

 

extract(array('a' = > array('a' => 'not this'), 'b' => 'bus'))

 

doesn't work

 

 

//edit fix`d

 

idk why i was evaling...

 

 <?php function newfn(array $data) {
    foreach($data as $key => $value) {
        $$key = $value; //works fine
    }
    // whatever here
} ?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.