TQ
dev.com

Blog about software development

Subscribe

Parameter 2 expected to be a reference

29 Apr 2016 - by 'Maurits van der Schee'

After my recent upgrade to Ubuntu 16.04 I have been making sure all my open source PHP projects work on PHP7. I am thrilled about PHP7 as it is about 2x faster than PHP5. In my experience it brings PHP more or less on par with NodeJS in terms of execution speed.

PHP Warning for "bind_param" and "bind_result"

When I was porting my PHP MVC framework (MindaPHP) to PHP7 I ran into the following warning:

PHP Warning:  Parameter 2 to mysqli_stmt::bind_param() 
expected to be a reference, value given in ...

It turns out I was using a dynamic function call using "call_user_func_array":

call_user_func_array(array($query, 'bind_param'),$args);

Where in the following (albeit longer) alternative PHP7 does not emit a warning:

$ref = new \ReflectionClass('mysqli_stmt');
$ref->getMethod("bind_param")->invokeArgs($query,$args);

Note that I do convert the $args variable to a array of references using:

foreach (array_keys($args) as $i) {
  if ($i>0) $args[$i] = & $args[$i];
}

This code is found in the database helper class of my MindaPHP MVC framework.

Porting to PHP7

It was the only adjustment I had to make in the following 3 projects (with over 4000 lines of code) to make them compatible with PHP7.

If I run into other issues then I will report them on this blog, so keep visiting!


PS: Liked this article? Please share it on Facebook, Twitter or LinkedIn.