Config array?

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

What are your thoughts on a technique like this:

<?php
$config
= array('username' => 'whatever', "first_name" => 'John', "last_name" => "Doe");

$user = new User($config);
?>

as opposed to:

<?php
$user
= new User('whatever', 'John', 'Doe');
?>

I started using the first technique, but I can't say I particularly like it. Too much typing! Which technique do you use?

EDIT: A user class probably isn't the best example of what I'm talking about, because you usually just pass it an ID, but I think you get the picture Smiling

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

That's how Codeigniter loads config settings for a class.

Wordpress takes parameters in a format like this:

$user = new User('username=whatever&first_name=john&last_name=Doe');

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

Do you like those ways of passing parameters? What's the main advantage to that?

I can see a few disadvantages to the array/query string passing:

  • You must know the exact name of the key
  • Intellitips don't work (they are those little hints that appear when you start typing the parameters to a function)
  • It just looks messy and there is a lot more typing
  • The only advantage I can think of is that you don't have to remember the order of the parameters. But a good IDE would remind you with intellitips.

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

But a good IDE would remind you with intellitips.

What IDE do you use? I just use a config array most of the time, unless there are roughly 4 or less parameters.

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

It depends on what I'm doing, but UltraEdit Studio, Visual Studio, Eclipse, and Dreamweaver. All of them have some form of intellitips.

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

CakePHP also uses the array style.

In my opinion, you're choosing between a rock and a hard place. On the basis of flexibility, I'd go with the former. If your entities are small, well-specified and not likely to change, then I'd go with the latter.

Semantically, the array style is better because you're essentially creating and passing a hash table of properties. That's usually a good thing to do for at least three reasons.

  • Naming values with keys, and grouping properties together adds to readability and clarity. In your latter User() call, it's not at all obvious which of the second and third arguments are the first or last names.
  • The contents of the table can be handled specially by the receiver -- i.e. optional or variadic values, precedence, validation, etc. You can set default values with an array_merge() idiom; easily iterate over variadic arguments, and so on.
  • Grouped properties can easily be passed down the call chain using the same protocol, rather than retyping each value. For example, your User constructor might call some other function foo() with the passed properties as input. You could call foo($config), or you could call foo($username, $first_name, $last_name). If you have a deep call chain or big property lists (like most frameworks do), you don't want to express your properties in every call.

Basically, you get a great deal more flexibility using arrays. Especially over time, when your architecture changes. Imagine you have a deep chain of calls as in the third point above, whose calling convention you want to update, say by inserting a new property. It would take plenty more refactoring to fix every call, than to simply update the hash table.

That said, neither approach is pretty, compared to other languages. Common Lisp, for instance, provides a number of argument-handling facilities to accept named (keyword) arguments, variadic (rest/whole) arguments, destructuring arguments, macros to manipulate them, and plenty more. Many of those features are also in Clojure, with even more powerful destructuring, and a nice hash table syntax. Fragments of those can be found in Ruby or Python, if you prefer.

Good luck.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.