matthewtbaker Posted October 25, 2012 Share Posted October 25, 2012 Hi, I'm building a function and was wondering if there's a method to present options to the developer when calling it. For instance I have a function which can return either HTTP Header Reponse Code, Full HTTP Header or HTML Body. When declaring the function I have function Return_Web_Content($url, $returnwhat) { switch ($returnwhat) case 'HTTP Header' : return $header; break; and so on... } So when calling the function during coding is there a way for have the options 'HTTP Header Response Code, Full HTTP Header and HTML Body presented instead of just writing the values into a string? Many thanks Matt Quote Link to comment https://forums.phpfreaks.com/topic/269900-create-a-function-with-options/ Share on other sites More sharing options...
Psycho Posted October 25, 2012 Share Posted October 25, 2012 (edited) What? Your question really doesn't make sense the way you have stated it, but I am going to venture a guess as to what you mean. I *think* you are talking about the "intellisense" functionality that some editors have built into them. For example when I type "substr(" into my editor I get a flyout menu that shows me there are three parameters for the $string, $start index and the optional $length parameter. If that is what you are talking about, then the answer is no. There is nothing you can do to control that for others as that is controlled completely by the persons editor. Each editor is different. On some you might be able to add some configuration settings manually to make it work, while others can dynamically provide that functionality for custom functions - assuming that the function is "loaded" in the editor. And, some editors may provide the specific flags available as parameters for some built-in PHP functions, but I can't think of how they could dynamically determine that for a custom function. It would require the PHP editor to be able to read the code and 'understand' it as a human would, Edited October 25, 2012 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/269900-create-a-function-with-options/#findComment-1387686 Share on other sites More sharing options...
kicken Posted October 25, 2012 Share Posted October 25, 2012 The closest you could get probably would be to use defined constants. A smart editor *might* be able to pick those up and offer them as completion suggestions. define('RETURN_RESPOSNECODE', 1); define('RETURN_FULLHEADER', 2); define('RETURN_FULLBODY', 3); function Return_Web_Content($url, $what){ switch ($what){ case RETURN_RESPONSECODE: ...; break; case RETURN_FULLHEADER: ...; break; case RETURN_FULLBODY: ...; break; } } Quote Link to comment https://forums.phpfreaks.com/topic/269900-create-a-function-with-options/#findComment-1387755 Share on other sites More sharing options...
Jessica Posted October 25, 2012 Share Posted October 25, 2012 I would make these three separate functions, since each section of the switch block does a different thing. They're not really related. Quote Link to comment https://forums.phpfreaks.com/topic/269900-create-a-function-with-options/#findComment-1387756 Share on other sites More sharing options...
Psycho Posted October 25, 2012 Share Posted October 25, 2012 @Kicken, I would be very surprised if any editor looks into a function to see what the parameters are used for and then provides intellisense when using the function in code. The function could just as easily have several if() conditions for those parameters. It could be done, but since it would only be a hack solution I don't see why anyone would bother. But, maybe I'm wrong. Quote Link to comment https://forums.phpfreaks.com/topic/269900-create-a-function-with-options/#findComment-1387788 Share on other sites More sharing options...
kicken Posted October 25, 2012 Share Posted October 25, 2012 I would be very surprised if any editor looks into a function to see what the parameters are used for and then provides intellisense when using the function in code. I would be too. I was thinking more along the lines of the editor picking up the define() lines and adding the constants to a list, then it could just auto-complete constant names for you. So if you were typing out Return_Web_Content($blah, RETU An editor could infer that the 'RETU' is a prefix to a constant and show a list of such constants as auto-complete options. Quote Link to comment https://forums.phpfreaks.com/topic/269900-create-a-function-with-options/#findComment-1387790 Share on other sites More sharing options...
requinix Posted October 25, 2012 Share Posted October 25, 2012 (edited) On that note I personally "namespace" the constants const PARAM_FOO = "foo"; const PARAM_BAR = "bar"; and then use PARAM as the type hint in the docblock. /** * Do something * * @param PARAM $p Thing to do */ public function doSomething($p) { Since the IDE can already pick up the docblock, an intelligent check for class constants named PARAM_* seems like a reasonable idea. Edited October 25, 2012 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/269900-create-a-function-with-options/#findComment-1387792 Share on other sites More sharing options...
matthewtbaker Posted October 26, 2012 Author Share Posted October 26, 2012 I would make these three separate functions, since each section of the switch block does a different thing. They're not really related. Thanks Jessica, these are however as a string is passed from a curl function run earlier. This function simply strips out the header or the body for seperate anaylsis. Quote Link to comment https://forums.phpfreaks.com/topic/269900-create-a-function-with-options/#findComment-1387861 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.