Only variables should be passed by reference

I have a class:

class Validator { private $validationArray; private $cleanedValues; public function __construct($arg1, $arg2=NULL) { if(empty($arg2)) { $this->LoadValidatorByName($arg1); } else { $this->LoadValidatorFromLeadType($arg1, $arg2); } } private function LoadValidatorFromLeadType($lead_type, $vocabulary) { $ErrorReporter = new ErrorReporter; $taxonomy_term = reset(taxonomy_get_term_by_name($lead_type, $vocabulary)); ...some more stuff 

The taxonomy_get_term_by_name function is a Drupal function, but the problem I'm experiencing is the PHP code.

When this method is called PHP, it complains:

 Strict warning: Only variables should be passed by reference in Validator->LoadValidatorFromLeadType() (line 32 of [path to my file]) 

Line 32 is the line with:

 $taxonomy_term = reset(taxonomy_get_term_by_name($lead_type, $vocabulary)); 

I looked at the error, and I'm sure I know what this means, but I canโ€™t understand what is wrong with my code, which causes this warning.

+4
source share
3 answers

reset expects a reference to a variable. You pass it the result of the function ...

 $taxonomy_term = taxonomy_get_term_by_name($lead_type, $vocabulary); $taxonomy_term = reset($taxonomy_term ); 
+12
source

This means that only the variable should be passed by reference, not by expression.

 reset($array_variable); // correct 

and

 reset(some_function_that_returns_array()); // incorrect 

If you take a second and think about it more, you will find that reset() with an expression (rather than a variable) does not make sense, because you rewind the array pointer to the beginning, but you do not have the ability to access this array anymore.

+8
source

You should only reset the variable (which is passed by reference) and not the return value of the function. see http://www.php.net/reset

+2
source

All Articles