maexus Posted April 18, 2008 Share Posted April 18, 2008 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. Quote Link to comment Share on other sites More sharing options...
dptr1988 Posted April 19, 2008 Share Posted April 19, 2008 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. Quote Link to comment Share on other sites More sharing options...
Acs Posted May 7, 2008 Share Posted May 7, 2008 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 Quote Link to comment Share on other sites More sharing options...
maexus Posted May 17, 2008 Author Share Posted May 17, 2008 Yes but that's not really what I want named arguments for. Using arrays for arguments seems like a mixed bag and I have found they work best when limited to a very need to use basis. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.