Config array?
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
pr0gr4mm3r posted this at 05:05 — 20th February 2009.
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 posted this at 03:04 — 21st February 2009.
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:
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 posted this at 05:47 — 21st February 2009.
He has: 1,502 posts
Joined: Sep 2006
What IDE do you use? I just use a config array most of the time, unless there are roughly 4 or less parameters.
teammatt3 posted this at 00:15 — 22nd February 2009.
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 posted this at 10:02 — 21st February 2009.
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.
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.