php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53731 Function arguments can only be changed when native values are given.
Submitted: 2011-01-12 22:07 UTC Modified: 2017-12-28 17:25 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: blue-tidus159 at hotmail dot com Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: Irrelevant OS: Windows 7 x64
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: blue-tidus159 at hotmail dot com
New email:
PHP Version: OS:

 

 [2011-01-12 22:07 UTC] blue-tidus159 at hotmail dot com
Description:
------------
The problem with debug_backtrace() is, that only function parameter values which are native types can be changed, but not if the value is an object.

Test script:
---------------
<?php

function errorHandler($level, $msg){
	$debug = debug_backtrace();
	$debug[1]['args'][0]='1';
	return true;
}

set_error_handler('error');

function a($a){ // E_RECOVERABLE_ERROR
	var_dump($a); 
}

class A{}

a(1);
a(2);
a(new A());

?>



Expected result:
----------------
int 1
int 1
int 1

Actual result:
--------------
int 1
int 1
object A

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-12 22:09 UTC] blue-tidus159 at hotmail dot com
Little typo, i meant:

set_error_handler('errorHandler');
 [2011-01-13 21:41 UTC] blue-tidus159 at hotmail dot com
I just realized that a similar effect can be reached by setting the reference mark before the parameter name. The problem is just that the original object will be changed too.

function a(&$a){
      var_dump($a);
}

$test = new A();
var_dump($test); // object A
a($test) // int 1
var_dump($test); // int 1 => this should still be object A, you should create internally a new reference parameter variable referencing the passed object if you know what i mean.

I know that native values get copied and that there is no problem with overwriting the value. But objects could be handled like this: if used as parameter value, create a new reference variable referencing the object, this variable can then be overwritten without changing the original object.
 [2015-08-28 11:43 UTC] nikic@php.net
-Status: Open +Status: Feedback
 [2015-08-28 11:43 UTC] nikic@php.net
I can't reproduce this. https://3v4l.org/QI9H8 shows that across all PHP versions it's not possible to modify the argument via debug_backtrace(). That's the correct behavior -- you aren't supposed to be able to do a modification through that. If you are able to do changes, that would be a bug.
 [2015-08-28 13:34 UTC] requinix@php.net
-Status: Feedback +Status: Open
 [2015-08-28 13:34 UTC] requinix@php.net
I think I have what @blue-tidus159 was trying to show. The key is that the function parameter has a type hint and the value passed is not an instance of it (thus triggering the error). But it gets a lot more interesting after that...
https://3v4l.org/7B3jB

Then the comment later seems to be about how it also happens with by-ref parameters.
https://3v4l.org/Vs8lG

On a lighter note, I have been inspired to create this abomination:
https://3v4l.org/5lbf0
 [2015-08-28 13:49 UTC] nikic@php.net
@requinix: Thanks, I didn't get that this is about type hints. In that case, I guess this is fixed in PHP 7 by means of it throwing an exception? It doesn't seem like doing this kind of modification through a direct call to debug_backtrace() is possible: https://3v4l.org/MWE8l
 [2017-12-28 17:25 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2017-12-28 17:25 UTC] nikic@php.net
As per previous comment, no longer relevant in PHP 7.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 11:01:30 2024 UTC