|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2000-12-05 20:28 UTC] wstockal at msysgroup dot com
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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 21:00:01 2025 UTC |
This problem can also affect the private copies of variables used inside of functions. Script: class test { var $property; function do_something(){ return; } } function mangle($array_in) { $array_in[0]->property = 'trashed'; } $array[0] = new test; $array[0]->property = 'ok'; mangle($array); echo("<h2>Before:</h2>\n"); echo("\$array[0]->property is {$array[0]->property}<br>\n"); $array[0]->do_something(); mangle($array); echo("<h2>After:</h2>\n"); echo("\$array[0]->property is {$array[0]->property}<br>\n"); Output: <h2>Before:</h2> $array[0]->property is ok<br> <h2>After:</h2> $array[0]->property is trashed<br>