Re: [PHP] trying to do too much with functions?

2004-09-02 Thread John Holmes
From: Justin French [EMAIL PROTECTED]
I have a few functions with way too many parameters in them, or functions 
which have a non-obvious order of parameters which I constantly have to 
refer to as I'm working.  Since I'm using PHP as my templating language 
(none smarty here!) as well as the programming language.  So I've been 
looking for some alternatives...

My main objectives:
  1.  have all parameters optional
  2.  have a mixed order for the paramaters
Other languages seem to do this well (Ruby, or perhaps RubyOnRails seems 
good at it), but in PHP I can only see a few options... I'm hoping someone 
might offer a few more ideas.

? $params = array('a'='123','b'='456'); echo doStuff($params); ?
?=doStuff(array('a'='123','b'='456'))?
?=doStuff('a=123','b=456')?
?=doStuff('a','123','b','456'); ?
?=doStuff(a='123' b='456')?
So, is that it?  Have I thought of all possible options?
Have you looked at the Function Handling Functions?
http://www.php.net/manual/en/ref.funchand.php
An array is probably your best bet, though, since you want to have mixed 
orders. The only way you'd be able to identify what parameter is which is by 
using the keys of the array, then.

---John  Holmes... 

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


Re: [PHP] trying to do too much with functions?

2004-09-02 Thread raditha dissanayake
Justin French wrote:
I have a few functions with way too many parameters in them, or 
functions which have a non-obvious order of parameters which I 
constantly have to refer to as I'm working.  Since I'm using PHP as my 
templating language (none smarty here!) as well as the programming 
language.  So I've been looking for some alternatives...
hey you forgot C :-)
now that java has also adapted a vargs look a like i guess it's high 
time for PHP to do the same. When faced with the find of situation that 
you are seem to face what I do is create a class and pass it along as a 
single parameter.

--
Raditha Dissanayake.

http://www.radinks.com/sftp/ | http://www.raditha.com/megaupload
Lean and mean Secure FTP applet with | Mega Upload - PHP file uploader
Graphical User Inteface. Just 128 KB | with progress bar.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] trying to do too much with functions?

2004-09-02 Thread Jason Wong
On Thursday 02 September 2004 22:11, Justin French wrote:
 I have a few functions with way too many parameters in them, or
 functions which have a non-obvious order of parameters which I
 constantly have to refer to as I'm working.  Since I'm using PHP as my
 templating language (none smarty here!) as well as the programming
 language.  So I've been looking for some alternatives...

 My main objectives:

1.  have all parameters optional
2.  have a mixed order for the paramaters

 Other languages seem to do this well (Ruby, or perhaps RubyOnRails
 seems good at it), but in PHP I can only see a few options... I'm
 hoping someone might offer a few more ideas.

 ? $params = array('a'='123','b'='456'); echo doStuff($params); ?
 ?=doStuff(array('a'='123','b'='456'))?
 ?=doStuff('a=123','b=456')?
 ?=doStuff('a','123','b','456'); ?
 ?=doStuff(a='123' b='456')?

 So, is that it?  Have I thought of all possible options?

Some languages handle it gracefully by allowing you to specify the parameters 
name and value when calling the function. AFAIK using arrays is the only way 
of emulating such a behaviour in PHP. Doing so allows you to easily extend a 
function's functionality and also not having to worry about order of 
parameters etc. Just look at how many of PHP's own functions have been 
extended over the years. And at least one of them have had the parameters 
switched around.

-- 
Jason Wong - Gremlins Associates - www.gremlins.biz
Open Source Software Systems Integrators
* Web Design  Hosting * Internet  Intranet Applications Development *
--
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-general
--
/*
Regarding astral projection, Woody Allen once wrote, This is not a bad way
to travel, although there is usually a half-hour wait for luggage.
*/

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



Re: [PHP] trying to do too much with functions?

2004-09-02 Thread Robby Russell
On Thu, 2004-09-02 at 07:11, Justin French wrote:
 I have a few functions with way too many parameters in them, or 
 functions which have a non-obvious order of parameters which I 
 constantly have to refer to as I'm working.  Since I'm using PHP as my 
 templating language (none smarty here!) as well as the programming 
 language.  So I've been looking for some alternatives...
 
 My main objectives:
 
1.  have all parameters optional
2.  have a mixed order for the paramaters
 
 Other languages seem to do this well (Ruby, or perhaps RubyOnRails 
 seems good at it), but in PHP I can only see a few options... I'm 
 hoping someone might offer a few more ideas.
 
 ? $params = array('a'='123','b'='456'); echo doStuff($params); ?
 ?=doStuff(array('a'='123','b'='456'))?
 ?=doStuff('a=123','b=456')?
 ?=doStuff('a','123','b','456'); ?
 ?=doStuff(a='123' b='456')?
 
 So, is that it?  Have I thought of all possible options?
 
 

/**
* pass an array..so as long as you pass one thing...
*/
function doStuff($args) 
{
  // do something 
  print_r($args);
}

/**
* this arg is optional as it has a default value
*/
function doSomething($x=0)
{
  return $x;
}


-Robby

-- 
/***
* Robby Russell | Owner.Developer.Geek
* PLANET ARGON  | www.planetargon.com
* Portland, OR  | [EMAIL PROTECTED]
* 503.351.4730  | blog.planetargon.com
* PHP/PostgreSQL Hosting  Development
/



signature.asc
Description: This is a digitally signed message part


Re: [PHP] trying to do too much with functions?

2004-09-02 Thread Jonathan Haddad
John Holmes elegantly wrote:
From: Justin French [EMAIL PROTECTED]
I have a few functions with way too many parameters in them, or 
functions which have a non-obvious order of parameters which I 
constantly have to refer to as I'm working.  Since I'm using PHP as 
my templating language (none smarty here!) as well as the programming 
language.  So I've been looking for some alternatives...

My main objectives:
  1.  have all parameters optional
  2.  have a mixed order for the paramaters
Other languages seem to do this well (Ruby, or perhaps RubyOnRails 
seems good at it), but in PHP I can only see a few options... I'm 
hoping someone might offer a few more ideas.

? $params = array('a'='123','b'='456'); echo doStuff($params); ?
?=doStuff(array('a'='123','b'='456'))?
?=doStuff('a=123','b=456')?
?=doStuff('a','123','b','456'); ?
?=doStuff(a='123' b='456')?
So, is that it?  Have I thought of all possible options?

Have you looked at the Function Handling Functions?
http://www.php.net/manual/en/ref.funchand.php
An array is probably your best bet, though, since you want to have 
mixed orders. The only way you'd be able to identify what parameter is 
which is by using the keys of the array, then.

---John  Holmes...
OR, if you really wanted to get crazy, you could do it like objective-C.
function blah()
{
   // contents
}
call the function like so
$classobject-function(firstname:jim, lastname:steve );
within the function, you could call func_get_args, then map your 
variables to an array using list($var, $val) = explode( :, $arg[x] )

of course, if you had a : (colon) in either string that exact solution 
wouldn't work.  but that's not the point of this lesson. 

i should note that I've considered doing this before and I didn't.  Now 
i just use default values of NULL.

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


Re: [PHP] trying to do too much with functions?

2004-09-02 Thread Matthew Weier O'Phinney
* Jonathan Haddad [EMAIL PROTECTED]:
 John Holmes elegantly wrote:
  From: Justin French [EMAIL PROTECTED]
   I have a few functions with way too many parameters in them, or 
   functions which have a non-obvious order of parameters which I 
   constantly have to refer to as I'm working.  
snip
   My main objectives:
  
 1.  have all parameters optional
 2.  have a mixed order for the paramaters
snip
   ? $params = array('a'='123','b'='456'); echo doStuff($params); ?
   ?=doStuff(array('a'='123','b'='456'))?
   ?=doStuff('a=123','b=456')?
   ?=doStuff('a','123','b','456'); ?
   ?=doStuff(a='123' b='456')?
  
   So, is that it?  Have I thought of all possible options?

I come from a perl background, which assumes an array is passed to a
subroutine. So, I personally like passing associative arrays to PHP
functions/methods, as this gives me very similar functionality. This
corresponds to the first two examples given by the OP.

However, regarding the following...

  Have you looked at the Function Handling Functions?
 
  http://www.php.net/manual/en/ref.funchand.php
snip
 OR, if you really wanted to get crazy, you could do it like objective-C.

 function blah()
 {
 // contents
 }

 call the function like so

 $classobject-function(firstname:jim, lastname:steve );

 within the function, you could call func_get_args, then map your 
 variables to an array using list($var, $val) = explode( :, $arg[x] )

 of course, if you had a : (colon) in either string that exact solution 
 wouldn't work.  but that's not the point of this lesson. 

Actually, as long as there are no colons in the variable name (key)
portion, you're fine -- simply do:

list($var, $val) = explode(':', $arg[x], 2);

which limits you to two return values from your explode() statement.
Unfortunately, what you're left with is:

$args = func_get_args();
for ($i = 0; $i  count($args); $i++) {
$list($var, $val) = explode(':', $args[$i], 2);
$$var = $val;
}

You could make a function out of the above loop:

function get_args($args) {
$arg = array();
for ($i = 0; $i  count($args); $i++) {
$list($var, $val) = explode(':', $args[$i], 2);
$arg[$var] = $val;
}
return $arg;
}

function do_something() {
$args = func_get_args(); $args = get_args($args); extract($args);
// do something now...
}

But, as you can see, you still end up with a lot of duplication and
chances to go wrong -- for instance, what if you forget to include the
file that has get_args() in it?

This is why I like OOP... :-)

-- 
Matthew Weier O'Phinney   | mailto:[EMAIL PROTECTED]
Webmaster and IT Specialist   | http://www.garden.org
National Gardening Association| http://www.kidsgardening.com
802-863-5251 x156 | http://nationalgardenmonth.org

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