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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 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

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.