php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44677 Function Parameters as non reference resets orignial Value
Submitted: 2008-04-09 10:01 UTC Modified: 2008-04-09 11:47 UTC
From: kudung at gmx dot net Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.5 OS: Debian 4
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: kudung at gmx dot net
New email:
PHP Version: OS:

 

 [2008-04-09 10:01 UTC] kudung at gmx dot net
Description:
------------
Hi,

just discovered that an parameter wich passed to an function as an non reference of type object is handeld as an reference,
but if i pass a parameter of type array to the function it is handled
as a copie as it should be.

Reproduce code:
---------------
class foob {
  function testbar( $das )
  {
    unset( $das->ding );
  }
}

function testbar( $das )
{
  unset( $das->ding );
}

$ers = new stdClass();
$ers->ding = '323';
testbar($ers);
var_dump( $ers );

$ers2 = new stdClass();
$ers2->ding = '323';
$foob = new foob();
$foob->testbar($ers2);
var_dump( $ers2  );



Expected result:
----------------
Expected that properties still exists:
object(stdClass)#1 (1) { ["ding"]=>  string(3) "323" } object(stdClass)#2 (1) { ["ding"]=>  string(3) "323" }

Actual result:
--------------
Output:
object(stdClass)#1 (0) { } 
object(stdClass)#2 (0) { }

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-04-09 11:25 UTC] jani@php.net
RTFM: http://www.php.net/manual/en/language.oop5.basic.php

"When assigning an already created instance of a class to a new variable, the new variable will access the same instance as the object that was assigned. This behaviour is the same when passing instances to a function. A copy of an already created object can be made by cloning it."
 [2008-04-09 11:47 UTC] kudung at gmx dot net
so therefore it is not possible to pass an param of type object into an function, without cloning it to be sure that the original object is not changed in any way?

but why does a param of type array get copied and can be altered without changing the original array?

this might be an improvement, but if its against any rule of oop so just throw this thougt away.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jun 17 18:01:31 2024 UTC