PHP: Passing ALL Arguments using a stdClass ?

  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post September 6th, 2009, 2:14 pm

I'm tempted to have all function declarations look something like the following in new code.

PHP Code: [ Select ]
function works_with(stdClass $args)
{
   echo $args->value1;
}
  1. function works_with(stdClass $args)
  2. {
  3.    echo $args->value1;
  4. }


Here are the benefits I can think of off the top of my head.

  • Named arguments
  • Argument order doesn't matter anymore
  • Multiple arguments can be optional, removing the need to pass a dummy value like null for earlier optional arguments when later ones are still used
  • Functions can be modified by simply adding the new code and having it look for its' argument in the stdClass, eliminating the need to refactor argument order and precedence

To simplify default values and make sure required arguments are included, it might be better to create argument classes and have functions expect them.

PHP Code: [ Select ]
<?php
 
class fun_args
{
   public $a, $b, $c;
   public function __construct()
   {
      $this->a = 'a';
      $this->b = 'b';
      $this->c = 'c';
      return $this;
   }
}
 
function fun(fun_args $args)
{
   echo "It's as easy as $args->a, $args->b, $args->c. ";
}
 
$lyric = new fun_args();
fun($lyric);
 
$lyric->a = 1;
$lyric->b = 2;
$lyric->c = 3;
fun($lyric);
 
// It's as easy as a, b, c. It's as easy as 1, 2, 3.
 
?>
  1. <?php
  2.  
  3. class fun_args
  4. {
  5.    public $a, $b, $c;
  6.    public function __construct()
  7.    {
  8.       $this->a = 'a';
  9.       $this->b = 'b';
  10.       $this->c = 'c';
  11.       return $this;
  12.    }
  13. }
  14.  
  15. function fun(fun_args $args)
  16. {
  17.    echo "It's as easy as $args->a, $args->b, $args->c. ";
  18. }
  19.  
  20. $lyric = new fun_args();
  21. fun($lyric);
  22.  
  23. $lyric->a = 1;
  24. $lyric->b = 2;
  25. $lyric->c = 3;
  26. fun($lyric);
  27.  
  28. // It's as easy as a, b, c. It's as easy as 1, 2, 3.
  29.  
  30. ?>


Is this something there's an existing design pattern for ?
Are there any blazing drawbacks I'm looking over ?
Strong with this one, the sudo is.
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post September 6th, 2009, 2:14 pm

  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post September 6th, 2009, 9:41 pm

I can't help you if it's a good idea or not, but I can say that it is interesting...
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6128
  • Loc: Seattle, WA
  • Status: Offline

Post September 8th, 2009, 4:52 pm

Frameworks such as CakePHP use arrays to pass most parameters. Same concept but doesn't require the use of a class to contain them.
The Beer Monocle. Classy.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post September 9th, 2009, 5:27 am

Advantage #1 of argument classes over associative arrays, class constructors and default values.
Strong with this one, the sudo is.
  • Nightslyr
  • Proficient
  • Proficient
  • No Avatar
  • Joined: Sep 21, 2005
  • Posts: 274
  • Status: Offline

Post September 28th, 2009, 3:20 pm

To be honest, I don't see any real benefit to it. It's a wrapper around an argument list. That kind of thing has its place (for instance, perhaps its beneficial to put a wrapper around $_REQUEST to facilitate validation and protect its values from accidental overwriting), but I don't think it's appropriate for all cases.

It certainly doesn't seem flexible. My code tends to have functions/methods that require a variety of different argument lists. I certainly wouldn't want to create a (sub)class for each one.

An example more realistic than a cute/canned Jackson 5 rendition may help foster discussion.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post September 28th, 2009, 4:19 pm

I've all but abandoned the idea.

I use it in a few choice places when I need default values or have a bunch of arguments, but as far as all arguments being objects it's just more trouble than it's worth.
Strong with this one, the sudo is.

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 121 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.