Jump to content

Create A Function With Options


matthewtbaker

Recommended Posts

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

Link to comment
Share on other sites

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 by Psycho
Link to comment
Share on other sites

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;
  }
}

 

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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 by requinix
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.