Reputation: 773
I am trying to properly comment my code, i usually don't echo out anything from within functions normally i just return data and then echo the returned value when needed. today I wrote a function that will be used very frequently with in view files only so I thought why not just echo from within the function and then call it when ever I need it and avoid typing echo everywhere.
I want to know if there is any way to indicate in phpDoc comment that this function will be echoing a string. Some thing like @sideEffect output or UpdateDb or SendToQueue?.
/**
* Echo error class
* @param array $fields
* @param string $field
* @return void
* @sideEffect output
*/
public static function getFailedClass($fields, $field)
{
if (isset($fields[$field])) {
echo 'failedValidation';
}
}
So can use in view like this:
<textarea name="data" class="<?php Lib\Validation::getFailedClass($fields, 'data'); ?>"></textarea>
Upvotes: 4
Views: 498
Reputation: 6688
No, there is no doc tag to indicate such behavior. At best, one would highlight in the description that such behavior occurs in the function.
An alternative you might consider is to have the function return rather than echo, and use <?=
short echo tags in your view. That way, you avoid writing functions with side effects (generally considered an anti-pattern) while still avoiding typing echo
at every use of the function. Best of both worlds :-)
Upvotes: 5