The Bat Posted August 10, 2007 Share Posted August 10, 2007 Hello, Is it at all possible in PHP to have "flexible" functions? For example, there's a function which has 3 parameters, and I only want to utilize 2 - the first one and last one. Instead of typing myFunc('first', false, 'third'); Could I/how would I do this? myFunc('first', $thirdparam = 'third'); (Assuming $thirdparam is the same variable used to declare the third parameter when creating the function.) Using Rails has had me wanting this functionality in PHP Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/64217-flexible-functions-a-possibility/ Share on other sites More sharing options...
Orio Posted August 10, 2007 Share Posted August 10, 2007 You can define a function that the number of parameters it can receives changes (check this). Maybe that helps you? Orio. Quote Link to comment https://forums.phpfreaks.com/topic/64217-flexible-functions-a-possibility/#findComment-320149 Share on other sites More sharing options...
btherl Posted August 10, 2007 Share Posted August 10, 2007 You can do this: function foo($args) { $bob = $args['bob']; $bill = &$args['bill']; } foo(array( 'bob' => 'hello', 'bill' => &$bubbles, )); Named arguments make life sooo much easier Quote Link to comment https://forums.phpfreaks.com/topic/64217-flexible-functions-a-possibility/#findComment-320160 Share on other sites More sharing options...
The Bat Posted August 12, 2007 Author Share Posted August 12, 2007 Thanks for the help! btherl, your method works great but I have one question: what exactly are the ampersands do? Quote Link to comment https://forums.phpfreaks.com/topic/64217-flexible-functions-a-possibility/#findComment-321452 Share on other sites More sharing options...
Foser Posted August 12, 2007 Share Posted August 12, 2007 For flexible functions you might want to get more involved in OOP. Quote Link to comment https://forums.phpfreaks.com/topic/64217-flexible-functions-a-possibility/#findComment-321454 Share on other sites More sharing options...
btherl Posted August 13, 2007 Share Posted August 13, 2007 The ampersands demonstrate how to use references when passing arguments as arrays. If you don't use references, you don't need to worry about them Just use the "bob" line for all your variables. If you do want references, the "bill" line in the example demonstrates that you must take a reference in two places for it to be effective. If you remove either one, then you will not get a reference back to the original variable. Quote Link to comment https://forums.phpfreaks.com/topic/64217-flexible-functions-a-possibility/#findComment-322017 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.