Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

register_shutdown_function converting references to values?

Tags:

php

reference

I'm trying to use a shutdown function that uses a value changed dynamically after registration. My understanding was that passing the variable by reference would allow changes in later portions of the code to affect the shutdown call. The code below demonstrates that this is not the case:

<?php
    $x = 0;
    function shutdown(&$x) {echo $x;}
    register_shutdown_function('shutdown',$x);
    $x = 1;
    exit();
?>

Am I misunderstanding how this should be done? Is it expected behavior for the register function to convert the reference pass to a value pass?

PHP version 5.5.9

like image 411
TheToolBox Avatar asked Jul 21 '14 23:07

TheToolBox


People also ask

What is Register_shutdown_function()?

Shutdown functions may also call register_shutdown_function () themselves to add a shutdown function to the end of the queue. The shutdown callback to register. The shutdown callbacks are executed as the part of the request, so it's possible to send output from them and access output buffers.

Can multiple shutdown functions be called at the same time?

Multiple calls to register_shutdown_function () can be made, and each will be called in the same order as they were registered. If you call exit () within one registered shutdown function, processing will stop completely and no other registered shutdown functions will be called.

What happens when you call exit() within a shutdown function?

If you call exit () within one registered shutdown function, processing will stop completely and no other registered shutdown functions will be called. Shutdown functions may also call register_shutdown_function () themselves to add a shutdown function to the end of the queue. The shutdown callback to register.

How to pass parameters to the shutdown function?

The shutdown callbacks are executed as the part of the request, so it's possible to send output from them and access output buffers. It is possible to pass parameters to the shutdown function by passing additional parameters. No value is returned. If the passed callback is not callable a E_WARNING level error will be generated.


1 Answers

One of possible solutions is to use anonymous functions (it's a closure in this case with $x captured as a reference):

$x = 0;

$shutdown = function() use(&$x) { echo $x; };

register_shutdown_function($shutdown);
$x = 1;

exit();

Demo: http://ideone.com/L2cYJp

UPD: Explanation why your solution doesn't work:

If you have a look on register_shutdown_function signature:

void register_shutdown_function ( callable $callback [, mixed $parameter [, mixed $... ]] )

you will see that parameters are passed by values. That's it - the value of $x in register_shutdown_function('shutdown',$x); line is passed by value and equals to the value it was on the moment of call.

If doesn't matter if you defined your function that accepts a reference since it's too late to accept a reference because you have already lost reference (indirectly, by using a function that does not take it into account).

like image 186
zerkms Avatar answered Nov 03 '22 17:11

zerkms