6

While in general hints are a good thing, there's a situation which I find pretty annoying and was wondering if there's an easy way around it.

Consider a function which has an output-only variable:

function dumb_foo($param1, $param2, &$out = null) {
  $out = $param1.'||'.$param2;
  return $param1*$param2;
}

Now making a call such as:

dumb_foo(5, 6, $my_out);

Results in a hint even though it's filled by the function. So yes, it's possible to initialize the variable first

$my_out = null;
dumb_foo(5, 6, $my_out);

but it's redundant.

Is there any other way to avoid the hint in this situation without removing it completely or adding an unneeded initialization?

Collector
  • 2,034
  • 4
  • 22
  • 39

3 Answers3

1

To the best of my knowledge, I think the best way is to first look at how pass by reference actually works.

case 1: Referenced variable $in already initialized but parameter$out NOT initialized

function increment(&$out) {
  $out++;
}

$in = 4 ; //NOTE: already initialized
increment($in);
echo $in; //add up to 5

case 2: Referenced variable $in NOT initialized but parameter$out initialized

    function increment(&$out = 4) {
      $out++;
    }

    //No initialization of $in
    increment($in);
    echo $in; // add up to 1
    //NOTE:No effect on referenced variable

case 3: Referenced variable $in NOT initialized and parameter$out NOT initialized

    function increment(&$out) {
      $out++;
    }

    //No initialization of $in
    increment($in);
    echo $in; //add up to 1

In my own opinion, case 3, will be a valid solution for the example you describe. Therefore removing the initialization of both $out and $my_out should do the work just fine. Something like this:

function dumb_foo($param1, $param2, &$out) {
  $out = $param1.'||'.$param2;
  return $param1*$param2;
}
dumb_foo(5, 6, $my_out);

Hope this helps!

xtrimzz
  • 59
  • 6
  • Thank you for answering but it doesn't seem like what you write is answering my question. There will be a hint either way. – Collector Feb 18 '16 at 08:23
0

I think you misunderstand how default values work. The issue is that $my_out isn't defined when you make the following call

dumb_foo(5, 6, $my_out);

So, you're actually passing in null (as opposed to not passing an argument at all), which means your default value is never used (coincidentally, your default value is also null). To illustrate the difference:

function dumb_foo(&$out = 'a') {
  print $out . "\n";
}

dumb_foo($i); // $i is not defined, so prints nothing
dumb_foo(); // prints 'a', the default value

In any case, you can turn off the "Unitialized Variable" hint completely:

Tools -> Options -> Editor -> Hints -> PHP -> Unitialized Variables
FuzzyTree
  • 32,014
  • 3
  • 54
  • 85
  • Thanks for your comment but it's not an answer to my question nor was it helpful. Did you consider that perhaps I added the default value so that I may choose to NOT pass a parameter at all if I have no need for it? – Collector Feb 18 '16 at 06:31
  • I was referring to "Results in a hint even though it's filled by the function." - this indicated to me that you misunderstood why the hint was being generated because the hint is generated right before the function is called, so it actually doesn't have anything to do with the function. If I'm mistaken then I apologize. – FuzzyTree Feb 18 '16 at 07:10
  • I meant that the function called is the one assigning the value so I have no need (on my side) to initialize it. So to your knowledge, PHP (nor NetBeans) don't have an option to define a variable as out-only to ensure the caller doesn't put a needless null in it? – Collector Feb 18 '16 at 08:27
0

Netbeans has an option under Tools -> Options -> Editor -> Hints -> PHP -> Uninitialized Variabled called Check Variables Initialized by Reference

Apparently is turned off by default because it affects the performance of the IDE.

More on this: NetBeans bug thread

Mihai Răducanu
  • 12,225
  • 4
  • 22
  • 31
  • Thanks for answering. Checking this option and looking at the source code didn't remove the hint. Unchecking and trying again made no difference. Have you tested this yourself? – Collector Feb 19 '16 at 19:47
  • OK, well, the hints are still there, whether I check the "Check Variables Initialized by Reference" or not, at least with NetBeans 8.02 and 8.1. I'll have to down vote this answer. – Collector Feb 22 '16 at 05:28