PHP: Passing ALL Arguments using a stdClass ?

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

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 ?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

I can't help you if it's a good idea or not, but I can say that it is interesting...
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

Frameworks such as CakePHP use arrays to pass most parameters. Same concept but doesn't require the use of a class to contain them.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

Advantage #1 of argument classes over associative arrays, class constructors and default values.
  • Nightslyr
  • Proficient
  • Proficient
  • Nightslyr
  • Posts: 283

Post 3+ Months Ago

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
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

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.

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 112 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.