PHP 4.0 Bug #8130: Changing a copy of an array affects the original

From: Date: Wed, 06 Dec 2000 01:28:28 +0000
Subject: PHP 4.0 Bug #8130: Changing a copy of an array affects the original
Groups: php.dev 
Request: Send a blank email to php-dev+get-40197@lists.php.net to get a copy of this message
From: wstockal@msysgroup.com Operating system: Redhat Linux 6.2 PHP version: 4.0.3pl1 PHP Bug Type: Arrays related Bug description: Changing a copy of an array affects the original A short script: <?php class test_class_2{ var $name; function distort_reality(){ echo("Method Called.<br>\n"); return; } } $a[] = new test_class_2; $a[0]->name = 'Original'; echo('$b = $a<br>'."\n"); $b = $a; echo("Original contains: {$a[0]->name}<br>\n"); echo("Copy contains: {$b[0]->name}<br><p>\n"); echo("Change contents for \$b<br>\n"); $b[0]->name = 'Copy'; echo("Original contains: {$a[0]->name}<br>\n"); echo("Copy contains: {$b[0]->name}<br><p>\n"); $a[0]->distort_reality(); echo('Once again, $b = $a<br>'."\n"); $b = $a; echo("Original contains: {$a[0]->name}<br>\n"); echo("Copy contains: {$b[0]->name}<br><p>\n"); echo("Change contents for \$b<br>\n"); $b[0]->name = 'Copy'; echo("Original contains: {$a[0]->name}<br>\n"); echo("Copy contains: {$b[0]->name}<br>\n"); ?> The output from the script: $b = $a Original contains: Original Copy contains: Original Change contents for $b Original contains: Original Copy contains: Copy Method Called. Once again, $b = $a Original contains: Original Copy contains: Original Change contents for $b Original contains: Copy Copy contains: Copy Basically, if $a is an array of objects and any method of an object is called any subsequent copy of $a becomes linked to the original. Any change to the copy propagates through to the original. Configure line: './configure' '--with-config-file-path=/usr/local/stronghold/conf' '--with-apxs=/usr/local/stronghold/bin/apxs' '--enable-versioning' '--disable-debug' '--with-zlib' '--with-gd=/usr/local' '--with-gdbm' '--with-db2' '--with-mysql' '--with-xml' '--with-ldap' '--with-cpdflib=/usr/local' '--with-png-dir=/usr' '--with-jpeg-dir=/usr' '--with-tiff-dir=/usr' '--with-xpm-dir=/usr/X11R6' '--enable-sysvshm=yes' '--enable-sysvsem=yes' '--enable-track-vars=yes' '--enable-bcmath=yes' '--enable-memory-limit=yes' Running as a module under apache 1.3.14 -- Edit Bug report at: http://bugs.php.net/?id=8130&edit=1

« previous php.dev (#40197) next »