Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phpdoc standard for setting default value of an optional parameter?

Tags:

php

phpdoc

Example:

/**
 * This function will determine whether or not one string starts with another string.
 * @param string $haystack <p>The string that needs to be checked.</p>
 * @param string $needle <p>The string that is being checked for.</p>
 * @param boolean $case[optional] <p>Set to false to ignore case(capital or normal characters)</p>
 * @return boolean <p>If the $haystack string does start with the $needle string, the return will be true. False if not.</p>
 */
function endsWith($haystack,$needle,$case=true) {
    if($case){return (strcmp(substr($haystack, strlen($haystack) - strlen($needle)),$needle)===0);}
    return (strcasecmp(substr($haystack, strlen($haystack) - strlen($needle)),$needle)===0);
}

The optional parameter is set to true by default. I wish to indicate what the default setting is in the documentation. Is there a standard way of doing this or do I have to mention it in the description?

like image 855
KdgDev Avatar asked Sep 06 '09 22:09

KdgDev


People also ask

What is default or optional parameter?

The default value of an optional parameter is a constant expression. The optional parameters are always defined at the end of the parameter list.

What does @param mean in PHP?

The @param tag is used to document a single argument of a function or method.

What is PHP default argument?

By default, function arguments are passed by value (so that if the value of the argument within the function is changed, it does not get changed outside of the function). To allow a function to modify its arguments, they must be passed by reference.


1 Answers

The doc says:

Note that the $paramname,... will be shown in the output docs in both the parameter listing AND the function signature. If you are not indicating in the actual code that the parameter is optional (via "$paramname = 'a default value'"), then you should mention in the parameter's description that the parameter is optional.

So if you're not showing the default assignment in the function signature, it would be a good idea to include it in the description, but in your case you are including it in the signature. So, you don't need to change a thing unless doing so would make you feel better.

like image 114
karim79 Avatar answered Oct 13 '22 12:10

karim79