It fires out when I try to call function with argument by reference
function test(&$a) ...
through
call_user_func('test', $b);
asked Sep 3, 2010 at 15:19
call_user_func
can only pass parameters by value, not by reference. If you want to pass by reference, you need to call the function directly, or use call_user_func_array
, which accepts references (however this may not work in PHP 5.3 and beyond, depending on what part of the manual look at).
answered Sep 3, 2010 at 15:22
3
From the manual for call_user_func()
Note that the parameters for call_user_func() are not passed by reference.
So yea, there is your answer. However, there is a way around it, again reading through the manual
call_user_func_array('test', array(&$b));
Should be able to pass it by reference.
Artefacto
96k17 gold badges200 silver badges225 bronze badges
answered Sep 3, 2010 at 15:23
JimJim
18.7k5 gold badges48 silver badges65 bronze badges
4
I’ve just had the same problem, changing (in my case):
$result = call_user_func($this->_eventHandler[$handlerName][$i], $this, $event);
to
$result = call_user_func($this->_eventHandler[$handlerName][$i], &$this, &$event);
seem to work just fine in php 5.3.
It’s not even a workaround I think, it’s just doing what is told
answered Mar 14, 2011 at 12:08
rhalffrhalff
1812 silver badges3 bronze badges
1
You need to set the variable equal to the result of the function, like so…
$b = call_user_func('test', $b);
and the function should be written as follows…
function test($a) {
...
return $a
}
The other pass by reference work-a-rounds are deprecated.
answered Sep 30, 2012 at 8:33
reubanoreubano
5,0191 gold badge40 silver badges41 bronze badges
You might consider the closure concept with a reference variable tucked into the «use» declaration. For example:
$note = 'before';
$cbl = function( $msg ) use ( &$note )
{
echo "Inside callable with $note and $msgn";
$note = "$msg has been noted";
};
call_user_func( $cbl, 'after' );
echo "$noten";
Bit of a workaround for your original problem but if you have a function that needs call by reference, you can wrap a callable closure around it, and then execute the closure via call_user_func().
answered Aug 29, 2019 at 8:30
Solution 1
call_user_func
can only pass parameters by value, not by reference. If you want to pass by reference, you need to call the function directly, or use call_user_func_array
, which accepts references (however this may not work in PHP 5.3 and beyond, depending on what part of the manual look at).
Solution 2
From the manual for call_user_func()
Note that the parameters for call_user_func() are not passed by reference.
So yea, there is your answer. However, there is a way around it, again reading through the manual
call_user_func_array('test', array(&$b));
Should be able to pass it by reference.
Solution 3
I’ve just had the same problem, changing (in my case):
$result = call_user_func($this->_eventHandler[$handlerName][$i], $this, $event);
to
$result = call_user_func($this->_eventHandler[$handlerName][$i], &$this, &$event);
seem to work just fine in php 5.3.
It’s not even a workaround I think, it’s just doing what is told
Solution 4
You need to set the variable equal to the result of the function, like so…
$b = call_user_func('test', $b);
and the function should be written as follows…
function test($a) {
...
return $a
}
The other pass by reference work-a-rounds are deprecated.
Comments
-
It fires out when I try to call function with argument by reference
function test(&$a) ...
through
call_user_func('test', $b);
Recents
Answer by Tadeo Hudson
Meta Stack Overflow
,
possible duplicate of Warning: mysql_fetch_* expects parameter 1 to be resource, boolean given error
– John Conde
Jul 28 ’12 at 16:39
,
Stack Overflow
Public questions & answers
,
Stack Overflow
help
chat
Check the function signature of Some_Function_name()
, you probably have something like:
function Some_Function_name(&$param1)
{
// ...
Answer by Maren Preston
Support » Fixing WordPress » Warning: Parameter 1 to wp_default_scripts() expected to be a reference,The topic ‘Warning: Parameter 1 to wp_default_scripts() expected to be a reference’ is closed to new replies.,Warning: Parameter 1 to wp_default_styles() expected to be a reference, value given in /nas/content/staging/stgsborg/wp-includes/class-wp-hook.php on line 286,Warning: Parameter 1 to wp_default_scripts() expected to be a reference, value given in /nas/content/staging/stgsborg/wp-includes/class-wp-hook.php on line 286
Just to add, I had to change another line in the same file referenced above by @mucha, around line 926 I had another instance of the ..( &$scripts ). So change:
function wp_default_styles( &$styles ) {}
becomes:
function wp_default_styles( $styles ) {}
function wp_default_styles( &$styles ) {}
Just to add, I had to change another line in the same file referenced above by @mucha, around line 926 I had another instance of the ..( &$scripts ). So change:
function wp_default_styles( &$styles ) {}
becomes:
function wp_default_styles( $styles ) {}
function wp_default_styles( $styles ) {}
Answer by Joaquin Jennings
This is a PHP 5.3 compatibility issue. You could remove the reference operator & from the argument $param1. Or you could rollback to PHP 5.2.x if absolutely necessary.,If you find copyright violations, you can contact us at info-generacodice.com to request the removal of the content.,The Contents are licensed under creative commons.,Check the function signature of Some_Function_name(), you probably have something like:
I am «familiar» with PHP and my friend had his site broken with error:
Warning: Parameter 1 to Some_function_name() expected to be a reference,
value given in /.../public_html/includes/tng/tNG.class.php on line 219
line 219:
$ret = call_user_func_array($callBackFunction,$tempParam);
Answer by Thea Palmer
If you do NOT use a $this reference, then PHP 7.2.3 without UOPZ will return warnings:
PHP Warning: Parameter 1 to myfunction() expected to be a reference, value given.,@mszabo-wikia Confirmed with PHP 7.2.3. Without UOPZ extension, ONLY this code (using &$this) will work and NO issue warnings:,At this point, we suspect that UOPZ could be exposing a bug within Zend, but don’t have a definitive example that proves it.,@ASchmidt1 Are you sure you are not running afoul of the changed handling of $this in PHP 7.1 and above?
<?php
declare(strict_types=1);
function myfunction( &$anobject ) { return 'success'; }
function callmyfunction( $args ) {
var_dump( call_user_func_array( 'myfunction', $args ) .'2' ); // fails!
var_dump( call_user_func_array( 'myfunction', array( &$args[0] ) ) .'3'); //circumvention
}
class MyClass
{
function __construct() {
var_dump( call_user_func_array( 'myfunction', array( &$this ) ) .'1' ); //works
callmyfunction( array( &$this ) ); // loses array-embedded reference!
}
}
$myobject = new MyClass;
Answer by Wells Kennedy
I get the mentioned error when i enable Google Analytics module. I believe the problem is PHP5.3.1 which i have running the website.,however, I do NOT have GoogleAnalytics module installed!,Repro is easy:
1. Set up drupal on a site running PHP 5.3
2. Enable Google Analytics module.,I’m not running Google Analytics or WYSIWYG and still get the error:
If you want to call all load_profile() «hooks», you need to use a custom foreach loop with module_implements:
$hook = 'profile_load';
$profile_fields = array();
foreach (module_implements($hook) as $module) {
$function = $module . '_' . $hook;
$profile_fields = array_merge($profile_fields, $function($user));
}
Answer by Kamden Patterson
I am «familiar» with PHP and my friend had his site broken with error:,Check the function signature of Some_Function_name(), you probably have something like:,Care must be taken when using mysqli_stmt_bind_param() in conjunction with call_user_func_array(). Note that mysqli_stmt_bind_param() requires parameters to be passed by reference, whereas call_user_func_array() can accept as a parameter a list of variables that can represent references or values.,This is a PHP 5.3 compatibility issue. You could remove the reference operator & from the argument $param1. Or you could rollback to PHP 5.2.x if absolutely necessary.
I am «familiar» with PHP and my friend had his site broken with error:
Warning: Parameter 1 to Some_function_name() expected to be a reference,
value given in /.../public_html/includes/tng/tNG.class.php on line 219
line 219:
$ret = call_user_func_array($callBackFunction,$tempParam);
Answer by Orlando Foley
Warning: Parameter 1 to wp_default_scripts() expected to be a reference, value given in /wp-includes/class-wp-hook.php on line 286,Warning: Parameter 1 to wp_default_styles() expected to be a reference, value given in /wp-includes/class-wp-hook.php on line 286,#custom heartbeat — stop the warning from WPE
# see https://www.thetwopercent.co.uk/solved-warning-parameter-1-to-wp_default_scripts/
define( ‘WPE_HEARTBEAT_INTERVAL’, 15 );,This error is caused because WP Engine turn off the WordPress heartbeat on Admin pages that are not editable.
Are you seeing a warning message in WordPress admin via Query Monitor?
Warning: Parameter 1 to wp_default_scripts() expected to be a reference, value given in /wp-includes/class-wp-hook.php on line 286
Warning: Parameter 1 to wp_default_scripts() expected to be a reference, value given in /wp-includes/class-wp-hook.php on line 286
Answer by Cora Fletcher
Warning: Parameter 1 to monographs_node_check() expected to be a reference, value given in menu_execute_active_handler() (line 517 of …public_htmlincludesmenu.inc).,The problem occurs because Drupal uses call_user_func_array() in order to invoke the menu callback function and pass the parameters to it.,
Stack Exchange network consists of 178 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
,I got the answer from the support list, and all I needed to do was remove the reference for the node object in my callback function signature:
I got the answer from the support list, and all I needed to do was remove the reference for the node object in my callback function signature:
function monographs_node_check($node, $type)
The following code:
<?php
declare(strict_types=1);
function myfunction( &$anobject ) { return 'success'; }
function callmyfunction( $args ) {
var_dump( call_user_func_array( 'myfunction', $args ) .'2' ); // fails!
var_dump( call_user_func_array( 'myfunction', array( &$args[0] ) ) .'3'); //circumvention
}
class MyClass
{
function __construct() {
var_dump( call_user_func_array( 'myfunction', array( &$this ) ) .'1' ); //works
callmyfunction( array( &$this ) ); // loses array-embedded reference!
}
}
$myobject = new MyClass;
will result in:
Parameter 1 to myfunction() expected to be a reference, value given in sample.php on line 7
Stack trace:
1. {main}() sample.php:0
2. MyClass->__construct() sample.php:18
3. callmyfunction() sample.php:15
4. call_user_func_array:{sample.php:7}() sample.php:7
@mszabo-wikia Confirmed with PHP 7.2.3. Without UOPZ extension, ONLY this code (using &$this) will work and NO issue warnings:
var_dump( call_user_func_array( 'myfunction', array( &$this ) ) .'1' );
callmyfunction( array( &$this ) );
If you do NOT use a $this reference, then PHP 7.2.3 without UOPZ will return warnings:
PHP Warning: Parameter 1 to myfunction() expected to be a reference, value given.
So native PHP works, and ONLY works, if &$this is used in BOTH lines.
However, the moment UOPZ is active, that working PHP 7.2.3 code fails.
Interesting. Based on PHP bug 73751 I’d have expected such usage of $this
to trigger a warning irrespective of uopz
being present. Maybe PHP’s handling here is not fully consistent?
Running the code above is successful regardless of PHP version:
▶ docker run --rm -v $(pwd)/test:/root/test php:5.6 php /root/test/test.php
string(8) "success1"
string(8) "success2"
string(8) "success3"
~
▶ docker run --rm -v $(pwd)/test:/root/test php:7.0 php /root/test/test.php
string(8) "success1"
string(8) "success2"
string(8) "success3"
~
▶ docker run --rm -v $(pwd)/test:/root/test php:7.2 php /root/test/test.php
string(8) "success1"
string(8) "success2"
string(8) "success3"
This issue specifically affects the greater WordPress community, would love to help, but the comments are pretty terse regarding PHP_FUNCTION(uopz_call_user_func)
Is it possible that the parameter copying in util.c (uopz_caller_init) is mutating the parameters, or when its copying the function from the hashtable, that it’s losing some bit of metadata, such as that the parameter should be a reference?
I also looked into the zpp api to see if it could be an issue with the zend data types, which I’m still not convinced couldn’t be the issue.
@krakjoe Do you have office hours to discuss?
I and a colleague spent a good amount of time debugging this with gdb in hopes we could spot the issue. We even tried commenting out various bits of functionality we suspected within UOPZ, such as the callers_init bits that perform the memcpy, and much more. Each time we reran the test by @ASchmidt1 above on Php7.2, it failed, but building the same code against the Zend libs in 7.0 seems to pass.
Dockerfile:
FROM php:7.0
RUN mkdir -p /usr/local/src/uopz
COPY . /usr/local/src/uopz
WORKDIR /usr/local/src/uopz
RUN phpize &&
./configure &&
make &&
make test &&
make install &&
echo ";priority=5" > /usr/local/etc/php/conf.d/uopz.ini &&
echo "extension=uopz.so" >> /usr/local/etc/php/conf.d/uopz.ini
CMD sleep 3600
After running docker build -t uopz7 . ; docker run -d --rm --name uopz7 uopz7
gdbtest docker exec -ti uopz7 bash
root@c7b164ae20d9:/usr/local/src/uopz# php -v
PHP 7.0.32 (cli) (built: Sep 15 2018 04:37:22) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
root@c7b164ae20d9:/usr/local/src/uopz# php -m
[PHP Modules]
...
uopz
root@c7b164ae20d9:/usr/local/src/uopz# php test/test.php
string(8) "success1"
string(8) "success2"
string(8) "success3"
At this point, we suspect that UOPZ could be exposing a bug within Zend, but don’t have a definitive example that proves it.
We see Zend calls in the backtraces within gdb while calling the mostly commented out bits of UOPZ, which still produces failures on php7.2.
Shop WordPress books on Amazon.
After upgrading from PHP version 7.0 to 7.1 on my WordPress website hosted with DreamHost, I received the error message below above the header on my website.
Warning: Parameter 2 to wp_hide_post_Public::query_posts_join() expected to be a reference, value given in /home/xyz_vps/nafzinger.com/wp-includes/class-wp-hook.php on line 298
The solution
If you’re not using the WP Hide Post plugin, make a note of the error message, it should give you a hint of what plugin is causing the issue. In the case above, my hint was Parameter 2 to wp_hide_post_Public::query_posts_join(). You can also disable the problematic plugin.
Some code caused the error in the WP Hide Post plugin. I fixed it by connecting to my server via SFTP, then navigating to the WP Hide Post plug-in directory, as shown below.
/wp-content/plugins/wp-hide-post/public/class-wp-hide-post-public.php
- Open class-wp-hide-post-public.php and search &$.
- Remove the all & that appear before the $ and save your file.
- Refresh your website, and the problem should be fixed.
- If you’re using a caching plugin, you may need to force-purge your cache and try again.
PHP 7.1 doesn’t like & in front of $query parameters in functions.
Use Garrett Digital for help with SEO or WordPress problems, large or small.
Shop WordPress books on Amazon.