Jump to content

Passing arrays as a temp replacement for named arguements?


maexus

Recommended Posts

What is your thoughts on having users pass data to your functions using an array due to the lack of named arguments in PHP? Example:

 

$db = new DB();

$db->connect(array(

'host'=>'localhost',

'username'=>'jimjoe',

'password'=>'hamburgers',

'database'=>'random_db'

));

 

vs

 

$db = new DB();

$db->connect('localhost', 'jimjoe', 'hamburgers', 'random_db'));

 

I'm at work and just thinking about this. Thoughts? Good, bad? Horrible readability? I was also thinking you could pass the array from another object or variable, like a settings class

 

 

$db = new DB();

$db->connect($settings->get("db_connection_data")));

 

Something like that.

I have used arrays as replacements for function args. It works great and is a LOT more flexible if you are going to do weird things with your functions.

 

My only problem was that I quickly got tired of typing out the whole array each time I wanted to call the function. It a little more work to type then just a plain function call. But you could get used to it if you tried.

 

It actually improved readability for me, especially when I hadn't used a function in a while, the named args would remind me what was what without having to look at the function definition.

 

Bottom line: good flexibility, good readability, horribly slow to type.

  • 3 weeks later...

If you use Zend ide you can set up comments on the function and so when you type the function a little yellow tip will appear and you can see the names of the arguments as you typed them in.

This at least can help you out as you type in the function :P

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.