#33061 [Bgs->Opn]: Pass object by value then modify initialized sub-object: passes by reference

From: Date: Mon, 20 Jun 2005 21:30:34 +0000
Subject: #33061 [Bgs->Opn]: Pass object by value then modify initialized sub-object: passes by reference
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-80727@lists.php.net to get a copy of this message
 ID:               33061
 User updated by:  online at natweiss dot com
 Reported By:      online at natweiss dot com
-Status:           Bogus
+Status:           Open
 Bug Type:         Class/Object related
 Operating System: *
-PHP Version:      5CVS-2005-06-19
+PHP Version:      4.3.*, 4.4.0RC1
 Assigned To:      dmitry
 New Comment:

1) Bug still exists in PHP 4.4.0RC1.

2) Simplified example does not produce actual results consistent with
sniper at php.net's results in PHP 4.3.10 or 4.4.0RC1.

See output:

php -v
======
PHP 4.4.0RC1 (cli) (built: Jun 18 2005 21:49:33)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

Original Reproduce Code Output
==============================
member->val should be empty!
something Object
(
    [member] => something Object
        (
            [val] => 1
        )

)

Simplified Example
==================
<?php
class something{
	var $val = 0;
	}
function pass_by_value($value){
	$value->val=1;
	}
$object = new something;
var_dump($object);
pass_by_value($object);
var_dump($object);
?>

Actual Results of Simplified Example
====================================
object(something)(1) {
  ["val"]=>
  int(0)
}
object(something)(1) {
  ["val"]=>
  int(0)
}


Previous Comments:
------------------------------------------------------------------------

[2005-06-20 20:29:51] dmitry@php.net

PHP5 always pass and assign objects by reference.
You should set "zend.ze1_compatibility_mode=1" if you like PHP4
behavior.

------------------------------------------------------------------------

[2005-06-19 02:12:55] sniper@php.net

ATTENTION: This does NOT happen with PHP_4_4 !!

Simplified example:

<?php
class something {
  public $val = 0;
}
function pass_by_value($value) {
    $value->val = 1;
}
$object = new something;
var_dump($object);
pass_by_value($object);
var_dump($object);
?>

Expected result:
----------------
int(0)
int(0)

Actual result:
--------------
int(0)
int(1)


------------------------------------------------------------------------

[2005-05-19 02:39:30] online at natweiss dot com

Description:
------------
See reproduce code.

php.ini is stock / no changes.

Reproduce code:
---------------
<?php
// a class that does nothing
class something{
	function nothing(){
		}
	}
// a pass-by-value function that modifies a member's member
function pass_by_value($value){
	$value->member->val = 1;
	}
// create a something with a member something
$object = new something;
$object->member = new something;

// call nothing, then call pass_by_value and print results
$object->member->nothing();
echo "member->val should be empty!\n";
pass_by_value($object);
print_r($object);
?>

Expected result:
----------------
$object->member should be empty

Actual result:
--------------
$object->member->val == 1


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=33061&edit=1


Thread (11 messages)

« previous php.bugs (#80727) next »