php5 - possible bug discovered

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I think I have discovered a bug in php5.  If I haven't, I've discovered
a bug in the documentation or a bug in my brain.

php -v :-

PHP 5.2.3 (cli) (built: Jun 26 2007 15:38:48) 
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies

I've attached a code snippet for those that want to save it and try
running it and it's also pasted here :-

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
<?php

class Class_1
{
private $Text;

function __construct($Text)
	{
	$this->Text = $Text;
	}

function Set($Text)
	{
	$this->Text = $Text;
	}

function Display()
	{
	print $this->Text . "\n";
	}
}			// End Class_1

class Class_2
{
private $Text;
private $C1;

function __construct($Text)
	{
	$this->Text = $Text;

	$this->C1 = new Class_1('From David');
	}

function Set_C1($Text)
	{
	$this->C1->Set($Text);
	}

function Display()
	{
	print $this->Text . "\n";
	$this->C1->Display();
	}
}			// End Class_2

print "Below is T1\n";

$T1 = new Class_1('Hello');
$T1->Display();

print "Below is T2\n";
$T2 = new Class_2('World');
$T2->Display();

print "Copying T2\n";

$T3 = $T2;

print "T2 Copied, original value is :-\n";

$T2->Display();

print "Copy of T2 (T3) value is :- \n";

$T3->Display();

print "Changing T3\n";

$T3->Set_C1("This clears Dave");

print "T3 Change complete, new value is :-\n";

$T3->Display();

print "Below is T2 which shouldn't have changed !!! \n";

$T2->Display();

// var_dump($T1);

?>

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

The output it produces is :-

% php -e bug.php
Below is T1
Hello
Below is T2
World
>From David
Copying T2
T2 Copied, original value is :-
World
>From David
Copy of T2 (T3) value is :- 
World
>From David
Changing T3
T3 Change complete, new value is :-
World
This clears Dave
Below is T2 which shouldn't have changed !!! 
World
This clears Dave
%

Now I would expect that $T2 would not change when I changed $T3.
Reading the documentation :-

<http://www.php.net/manual/en/language.references.whatdo.php>

"... Note:  Not using the & operator causes a copy of the object to be
made. If you use $this in the class it will operate on the current
instance of the class. The assignment without & will copy the instance
(i.e. the object) and $this will operate on the copy, which is not
always what is desired. Usually you want to have a single instance to
work with, due to performance and memory consumption issues."

This effectively states that $T3 is a 'COPY' of $T2 and $T3 works on the
copy, not the master yet in the above snippet, the copy and the master
have both been changed.  Further documentation mentions that the copy
starts off as a reference and only becomes a copy when it is changed.
This doesn't seem to be happening.

I found this because in my actual application I was using :-
Master.class
...
$Clone = $this;

$Clone->Do_Something;


and later in master.class I did :-

$this->Do_Something_Else

and php barfed with :-

PHP Fatal error:  Call to a member function Function_Name() on a non-object 

I had deleted the missing object in $Clone but not in the master (this
was the whole idea of working on $Clone - I didn't want to delete the
object in the master !!).

I can't find anything similar in the bug database on php.net (but I may
be looking in the wrong place) and I can't believe that I'm the first
person to encounter this.

Can anybody explain if this is a bug or if I have misunderstood the
documentation ?

TTFN



D
php/general-2007-10-08.tx                                      php-general
+----------------------------------------------------------------------------+
| Dave Restall, Computer Nerd, Cyclist, Radio Amateur G4FCU, Bodger          |
| Mob +44 (0) 7973 831245      Skype: dave.restall             Radio: G4FCU  |
| email : dave@xxxxxxxxxxx                     Web : Not Ready Yet :-(       |
+----------------------------------------------------------------------------+
| Good salesmen and good repairmen will never go hungry.                     |
|         -- R. E. Schenk                                                    |
+----------------------------------------------------------------------------+

Attachment: bug.php
Description: application/httpd-php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux