Re: [PHP] How can I use a function's default arguments but change one of the end ones in the list?

2009-02-06 Thread Frank Stanovcak

""Boyd, Todd M.""  wrote in message 
news:33bde0b2c17eef46acbe00537cf2a19003d07...@exchcluster.ccis.edu...
> -Original Message-
> From: Daevid Vincent [mailto:dae...@daevid.com]
> Sent: Friday, February 06, 2009 2:26 PM
> To: php-general@lists.php.net
> Subject: Re: [PHP] How can I use a function's default arguments but
> change one of the end ones in the list?
>
> On Fri, 2009-02-06 at 14:10 +1300, German Geek wrote:
>
> > I've thought about this problem before but couldn't think of a
> > solution either.
>
>
> Well, I guess I *did* think of a solution, it just happens that PHP
> doesn't do "the logical thing"(tm)
>
> ;-)
>
> I would hope the PHP developers would implement this idea for a future
> version. The concept is simple, just specify which parameters in the
> function call you want to change and leave the others as their
default.
>
> I've added it here: http://bugs.php.net/bug.php?id=47331 with comments
> about similar/related bugs.
>
> Feel free to vote on this. I think it's a necessary addition to the
> language and often a source of frustration for me in large projects
> especially where functions get older and older and more parameters are
> needed to maintain backwards compatibility yet get new functionality
> too.

Just tell 'em that .NET already has it; that'll light a fire under them!
:D
C# can do function calls like:

FunctionName(VariableName:=Value);

I wholeheartedly prefer PHP to .NET for most things... but you've got to
admit--that's a pretty slick feature. Me likey.


// Todd

I don't remember what it was I was using, but I remember a lang that allowed 
func(,,,change,,,change) where all the empty comas just went to default. 
*shrug* 



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] How can I use a function's default arguments but change one of the end ones in the list?

2009-02-06 Thread Boyd, Todd M.
> -Original Message-
> From: Daevid Vincent [mailto:dae...@daevid.com]
> Sent: Friday, February 06, 2009 2:26 PM
> To: php-general@lists.php.net
> Subject: Re: [PHP] How can I use a function's default arguments but
> change one of the end ones in the list?
> 
> On Fri, 2009-02-06 at 14:10 +1300, German Geek wrote:
> 
> > I've thought about this problem before but couldn't think of a
> > solution either.
> 
> 
> Well, I guess I *did* think of a solution, it just happens that PHP
> doesn't do "the logical thing"(tm)
> 
> ;-)
> 
> I would hope the PHP developers would implement this idea for a future
> version. The concept is simple, just specify which parameters in the
> function call you want to change and leave the others as their
default.
> 
> I've added it here: http://bugs.php.net/bug.php?id=47331 with comments
> about similar/related bugs.
> 
> Feel free to vote on this. I think it's a necessary addition to the
> language and often a source of frustration for me in large projects
> especially where functions get older and older and more parameters are
> needed to maintain backwards compatibility yet get new functionality
> too.

Just tell 'em that .NET already has it; that'll light a fire under them!
:D
C# can do function calls like:

FunctionName(VariableName:=Value);

I wholeheartedly prefer PHP to .NET for most things... but you've got to
admit--that's a pretty slick feature. Me likey.


// Todd

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] How can I use a function's default arguments but change one of the end ones in the list?

2009-02-06 Thread Daevid Vincent
On Fri, 2009-02-06 at 14:10 +1300, German Geek wrote:

> I've thought about this problem before but couldn't think of a
> solution either. 


Well, I guess I *did* think of a solution, it just happens that PHP
doesn't do "the logical thing"(tm) 

;-)

I would hope the PHP developers would implement this idea for a future
version. The concept is simple, just specify which parameters in the
function call you want to change and leave the others as their default.

I've added it here: http://bugs.php.net/bug.php?id=47331 with comments
about similar/related bugs.

Feel free to vote on this. I think it's a necessary addition to the
language and often a source of frustration for me in large projects
especially where functions get older and older and more parameters are
needed to maintain backwards compatibility yet get new functionality
too.




Re: [PHP] How can I use a function's default arguments but change one of the end ones in the list?

2009-02-06 Thread Alpár Török
2009/2/6 Daevid Vincent 

> Is there a way to use the default values of a function without
> specifying every single one until the parameter you want to modify in
> PHP5 ?
>
> I don't see it here, but feel this would be very useful indeed.
> http://www.php.net/manual/en/functions.arguments.php
>
> So given a function that takes seven parameters, I want to change one of
> them and leave the other defaults alone...
>
>  function SQL_QUERY($sql, $parameters = null, $showSQL = false,
> $showErrors = true, $execute = true, $noHTML = false, $profile = 0)
> {
>var_dump($sql, $parameters, $showSQL, $showErrors, $execute,
> $noHTML,
> $profile);
> }
> ?>
>
> 
> 
> 
> 
> 
>  true); ?>
> 
> 
> 
>  $execute=false); ?>
> 
>
> outputs:
> ---
> string(31) "SELECT * FROM foo WHERE bar = ?"
> NULL
> bool(false)
> bool(true)
> bool(true)
> bool(false)
> int(0)
> ---
> string(31) "SELECT * FROM foo WHERE bar = ?"
> array(1) {  [0]=>  string(4) "beep" }
> bool(true)
> bool(true)
> bool(true)
> bool(false)
> int(0)
> ---
> string(31) "SELECT * FROM foo WHERE bar = ?"
> array(1) {  [0]=>  string(4) "beep" }
> bool(false)
> bool(true)
> bool(true)  <-- I would have expected this one to be bool(false)
> bool(false)
> int(0)
>
> The above function call doesn't error out on me, it just seems it
> doesn't do anything either :-\
>
> So it seems I have to do this verboseness (AND know what the default
> values are to begin with too):
>
> SQL_QUERY('SELECT * FROM foo WHERE bar = ?', array('beep'), false, true,
> false, false);
>
> Just to change one default parameter?!? :-(
>
>
>

You could probably create  a class and break up the functionality of that
function, for somebody reading your code would be hard understanding all
that parameters, maybe even for you after a while.


-- 
Alpar Torok


Re: [PHP] How can I use a function's default arguments but change one of the end ones in the list?

2009-02-05 Thread Daniel Brown
On Thu, Feb 5, 2009 at 20:10, German Geek  wrote:
> I've thought about this problem before but couldn't think of a solution
> either. How does func_get_args() solve this? You could make a wrapper
> function without that.

It's all in how it's used.  Ironically, I don't remember saying
that you couldn't make a wrapper function without it, or that it was
required to make a wrapper.  Thanks for clarifying that for everyone
just in case.  ;-P

> How would u (php) know which parameter u mean in a particular case?

You don't have to.  You can send NULL or empty strings for that
matter, and use the parameter position in func_get_args() that way.
Or using hierarchy and preference:



Though inelegant, it works.

What would probably be better, though, would be to write the
wrapper as I mentioned, but have it accept a single parameter as an
array, like so:

 $v) {
$$k = $v;
}

// Call original function with these values and pass through the results.
return orig_func($param_one,$param_two,$param_three,$param_four);
}

// Then use it like so:
$params = array(
'param_two' => 'Orange',
'param_four' => 543.21,
);
?>

If all orig_func() did was a simple foreach()/echo routine, it
wrapper_func() would print:

[NULL]
Orange
foo
543.21

> I think it would just be useful to have an IDE that can write out the
> default parameters on a keyboard shortcut or mouse click and then u can
> change them afterwards.

It would be nice if every programmer touching the code had the
same IDE.  Unfortunately, it's just not practical.

-- 

daniel.br...@parasane.net || danbr...@php.net
http://www.parasane.net/ || http://www.pilotpig.net/
Unadvertised dedicated server deals, too low to print - email me to find out!

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] How can I use a function's default arguments but change one of the end ones in the list?

2009-02-05 Thread Shawn McKenzie
German Geek wrote:
> I've thought about this problem before but couldn't think of a solution
> either. How does func_get_args() solve this? You could make a wrapper
> function without that.
> 
> How would u (php) know which parameter u mean in a particular case?
> 
> I think it would just be useful to have an IDE that can write out the
> default parameters on a keyboard shortcut or mouse click and then u can
> change them afterwards.
> 
> Cheers,
> Tim
> 
> Tim-Hinnerk Heuer
> 
> http://www.ihostnz.com
> 

Well, using func_get_args() you can pass whatever you want and parse the
args to see what was passed and assign default values if a specific arg
wasn't passed.

I however would probably pass an associative array to the func and parse
that.
-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] How can I use a function's default arguments but change one of the end ones in the list?

2009-02-05 Thread Jim Lucas
Daevid Vincent wrote:
> Is there a way to use the default values of a function without
> specifying every single one until the parameter you want to modify in
> PHP5 ?
> 
> I don't see it here, but feel this would be very useful indeed.
> http://www.php.net/manual/en/functions.arguments.php
> 
> So given a function that takes seven parameters, I want to change one of
> them and leave the other defaults alone...
> 
 > The above function call doesn't error out on me, it just seems it
> doesn't do anything either :-\
> 
> So it seems I have to do this verboseness (AND know what the default
> values are to begin with too):
> 
> SQL_QUERY('SELECT * FROM foo WHERE bar = ?', array('beep'), false, true,
> false, false);
> 
> Just to change one default parameter?!? :-(
> 


What you are wanting to do is not possible.

Back in the day, I worked on a project that used our own variation of this type 
of functionality.

We had this function that helped a little


function alternate(&$a, $b) {
return $a = ( $a ? $a : $b );
}



function query($sql, $params=array()) {
alternate($params,  array());
extract($params);

alternate($parameters,  null);
alternate($showSQL, false);
alternate($showErrors,  true);
alternate($execute, true);
alternate($noHTML,  false);
alternate($profile, 0);

# Do your thing...

}

This allowed for us to include/exclude whatever we wanted from the

it is a little long winded to get to what you are looking for ( I think )


But it worked for us.




-- 
Jim Lucas

   "Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] How can I use a function's default arguments but change one of the end ones in the list?

2009-02-05 Thread German Geek
I've thought about this problem before but couldn't think of a solution
either. How does func_get_args() solve this? You could make a wrapper
function without that.

How would u (php) know which parameter u mean in a particular case?

I think it would just be useful to have an IDE that can write out the
default parameters on a keyboard shortcut or mouse click and then u can
change them afterwards.

Cheers,
Tim

Tim-Hinnerk Heuer

http://www.ihostnz.com


On Fri, Feb 6, 2009 at 1:56 PM, Daniel Brown  wrote:

> On Thu, Feb 5, 2009 at 18:50, Daevid Vincent  wrote:
> > Is there a way to use the default values of a function without
> > specifying every single one until the parameter you want to modify in
> > PHP5 ?
>
> Daevid,
>
>Check out func_get_args(): http://php.net/func_get_args
>
>Then you can rename your sql_query() function to real_sql_query()
> and create a new sql_query() as a wrapper function with
> func_get_args() in place of statically-defined variables.
>
> --
> 
> daniel.br...@parasane.net || danbr...@php.net
> http://www.parasane.net/ || http://www.pilotpig.net/
> Unadvertised dedicated server deals, too low to print - email me to find
> out!
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


Re: [PHP] How can I use a function's default arguments but change one of the end ones in the list?

2009-02-05 Thread Daniel Brown
On Thu, Feb 5, 2009 at 18:50, Daevid Vincent  wrote:
> Is there a way to use the default values of a function without
> specifying every single one until the parameter you want to modify in
> PHP5 ?

Daevid,

Check out func_get_args(): http://php.net/func_get_args

Then you can rename your sql_query() function to real_sql_query()
and create a new sql_query() as a wrapper function with
func_get_args() in place of statically-defined variables.

-- 

daniel.br...@parasane.net || danbr...@php.net
http://www.parasane.net/ || http://www.pilotpig.net/
Unadvertised dedicated server deals, too low to print - email me to find out!

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] How can I use a function's default arguments but change one of the end ones in the list?

2009-02-05 Thread Daevid Vincent
Is there a way to use the default values of a function without
specifying every single one until the parameter you want to modify in
PHP5 ?

I don't see it here, but feel this would be very useful indeed.
http://www.php.net/manual/en/functions.arguments.php

So given a function that takes seven parameters, I want to change one of
them and leave the other defaults alone...















outputs:
---
string(31) "SELECT * FROM foo WHERE bar = ?"
NULL
bool(false)
bool(true)
bool(true)
bool(false)
int(0)
---
string(31) "SELECT * FROM foo WHERE bar = ?"
array(1) {  [0]=>  string(4) "beep" }
bool(true)
bool(true)
bool(true)
bool(false)
int(0)
---
string(31) "SELECT * FROM foo WHERE bar = ?"
array(1) {  [0]=>  string(4) "beep" }
bool(false)
bool(true)
bool(true)  <-- I would have expected this one to be bool(false)
bool(false)
int(0)

The above function call doesn't error out on me, it just seems it
doesn't do anything either :-\

So it seems I have to do this verboseness (AND know what the default
values are to begin with too):

SQL_QUERY('SELECT * FROM foo WHERE bar = ?', array('beep'), false, true,
false, false);

Just to change one default parameter?!? :-(