I have this code for passing heavy variables among different classes. The goal is that there should be no duplication of values โโin the memory, so the variables are passed by reference instead of the value.
I have this sample here. It works great, except for one way. To make sure that the value is not duplicated in memory, I change the value or delete it to make sure.
Then I realized that UNSETing a variable has a different effect and making it NULL has a different effect.
If I UNSET the source variable, I still get the value from the reference variable. If I make the source variable NULL, it will not return anything.
Here is the code:
<?php class a { private $req; public function store_request(&$value) { $this->req = &$value; } public function get_request() { return $this->req; } } class b { private $json; private $obj; public function init_req(&$request) { $this->obj = new a; $this->obj->store_request($request); } public function get_req() { return $this->obj->get_request(); } } $locvar = 'i am here'; $b = new b; $b->init_req($locvar); $locvar = 'now here';
UNSET and NULL lines are numbered. If you are trying to run the code, do not combine one line at a time (I know this is stupid advice here, but still :))
Can anyone tell what is going on inside?
source share