PHP: How to properly copy a primitive to a new memory address -


i had issue (php: variable value mysteriously set 0) variable being passed value php function, , somehow value of variable changed, so:

$var = 'hello'; some_function($var); echo $var; // outputs 0 

it hypothesized it's old function written in c , in innards doing corrupt variable, since in c apparently can kind of thing. anyway, tried copy value first , pass in, so:

$var = 'hello'; $var2 = $var;  some_function($var2); echo $var; // still outputs 0 

of course didn't help, because, reading here php pointing $var2 in symbol table same zval container $var , increasing zval container's refcount. of course, if $var2 reassigned "by value", new zval container should created it, not regular assign-by-value, apparently (there's bit in previous question __set() magic method thing). presumed happening physical memory address @ value stored, rather assignment happening through normal php machinery, , pointing zval affected. well.

my workaround find way clone primitive $var. sorry if repeat, found tons of stuff on cloning classes nothing on cloning primitives, think because php supposed doing automatically. ended using this:

$var = 'hello';  $serialized_var = serialize($var); $var2 = unserialize($serialized_var);  some_function($var2); echo $var; // outputs 'hello', yay! 

i'm wondering, there better way? let's presume there's no other workaround, using different function or something, , have true copy made of variable. how 1 go it?


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -