0

Does PHP provide any Lazy copy concept?

My believe is that Lazy copy is not implemented in PHP(infact is it a correct terminology?) while Lazy loading can be implement on object properties by simple flag property of an object.

I came across a answer(Please see) on SO with a large number of upvote, a part of explanation seems to be completely wrong.

He is saying unless $b is not changed $a will keep only reference of $b.

$b=3;
$a=$b;
// $a points to $b, equals to $a=&$b 
$b=4;
// now PHP will copy 3 into $a, and places 4 into $b

I can understand Lazy loading. Keep a flag property in object and whenever we try to get the property of an object just initialize all properties from DB. Pseudo code looks like this:

private function GetAccessor($member) {

if($this->isLoaded != true) { 
$this->Load(); //initialize or copy all properties from DB - LAZY LOADING
}
....

Note: php.net also doesn't mentioned lazy copy anywhere.

Community
  • 1
  • 1
P K
  • 9,972
  • 12
  • 53
  • 99
  • *(tip)* [Copy on Write in the PHP Language](http://www.research.ibm.com/trl/people/mich/pub/200901_popl2009phpsem.pdf) – Gordon Jan 06 '12 at 23:23
  • @Gordon: yeah, also seen it and tried to read, but looks toooooo bloated and scientific ;-) – zerkms Jan 06 '12 at 23:28
  • @Gordon. seems to be a research paper, but worth to read. thanks. – P K Jan 06 '12 at 23:32

2 Answers2

4

My believe is that Lazy copy is not implemented

It is implemented and it is called COW (Copy-on-Write)

See:

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • what is Copy on write? can you please describe with a sample code. – P K Jan 06 '12 at 23:19
  • @Praveen: it is the correct name of the behaviour explained in *your* first code example – zerkms Jan 06 '12 at 23:20
  • so before $b=4, if i put echo $a; will lazy copy happen at echo statement? – P K Jan 06 '12 at 23:21
  • @Praveen: no. Lazy copy is performed when original variable is modified. Unless it is modified - both variables point literally to the same data in memory. – zerkms Jan 06 '12 at 23:22
  • @ zerkms, so you mean PHP supports lazy copy? – P K Jan 06 '12 at 23:23
  • @Praveen: added some references to my answer. I mean **php supports COW**, I don't precisely know what `lazy copy` means. – zerkms Jan 06 '12 at 23:24
  • @zerkms.thanks a lot for references, i will definitely look into COW concept. – P K Jan 06 '12 at 23:28
  • "[Lazy copy](http://en.wikipedia.org/wiki/Object_copy#Lazy_copy) is related to [copy-on-write](http://en.wikipedia.org/wiki/Copy-on-write)." – Wiseguy Jan 06 '12 at 23:29
4

Well yes PHP does this. This is an optimization strategy the php interpreter does for you. The concept is also known as "copy on write".

Suppose you have a reeeaaaallly large string $a = "lllloooooong [imagine another million characters here]"; And then you want to copy that: $b = $a; Then chances are, that doing this copy operation was never necessary, because either you never altered $a or $b meaning that both variables have the same value at all the time and thus you could just use $a OR $b reducing your memory consumption by 50% and saving you that copy operation.

So the PHP-interpreter will on the first operation $b = $a assume, that you probably will never change $a or $b and it will not do any copying but instead it memorizes that $b has the same data as $a. As soon as you change $b or as soon as you change $a, the interpreter's previous assumption is proven as wrong and the interpreter will do the copying after all.

But this behaviour is an operation that happens behind the scenes. You don't see it, you can't influence it directly and it does not have any effect that you must be aware of in order to code PHP. Instead you can always work as if variables would be copied immediately.

yankee
  • 38,872
  • 15
  • 103
  • 162