Skip to main content

All Questions

0 votes
0 answers
94 views

Adding new reference to any item in array affects behaviour of the array

I've seen many questions about this (one of them here) $a = array('a', 'b', 'c', 'd'); foreach ($a as &$v) { } foreach ($a as $v) { } print_r($a); and I get the answers, you shouldn't be ...
Vali's user avatar
  • 16
1 vote
0 answers
35 views

Are PHP zvals mutable?

I've been reading about memory management in PHP and learned that variables in PHP copy the reference to zvals as long as you don't do a write operation (copy on write paradigm). https://www....
tweekz's user avatar
  • 338
9 votes
2 answers
4k views

Is there ever a need to use ampersand in front of an object?

Since objects are passed by reference by default now, is there maybe some special case when &$obj would make sense?
LDusan's user avatar
  • 205
3 votes
1 answer
167 views

PHP ignoring returned reference from function considered harmful?

Derick Rethans has an old article that says: Please do note that it is harmful not to accept a reference from a function that returns a reference. In some cases, PHP will get confused and cause ...
Pacerier's user avatar
  • 88.6k
2 votes
1 answer
254 views

Why does a PHP array get modified when it's element is reference-assigned?

When ref-assigning an array's element, the contents of the array are modified: $arr = array(100, 200); var_dump($arr); /* shows: array(2) { [0]=> int(100) // ← ← ← int(100) [1]=> int(...
Pacerier's user avatar
  • 88.6k
1 vote
1 answer
107 views

Why C.O.W does not take place when ' writing to a property ' / ' injecting a property into an object ' of class?

class a { public $test="msg1"; } $t1 = new a; echo "echo1: After Instantiation :<br/>"; xdebug_debug_zval('t1');echo "<br/><br/>"; $t2 = $t1; echo 'echo2: After ...
ThinkingMonkey's user avatar
17 votes
2 answers
478 views

What is exactly happening when instantiating with 'new'?

Let's consider the following code: class a { public $var1; function disp(){ echo $this->var1; } } $obj1 = new a; echo '<br/>After instantiation into $obj1:&...
ThinkingMonkey's user avatar
8 votes
6 answers
4k views

PHP Performance : Copy vs. Reference

Hey there. Today I wrote a small benchmark script to compare performance of copying variables vs. creating references to them. I was expecting, that creating references to large arrays for example ...
fresskoma's user avatar
  • 25.7k