php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #51327 Perhaps there should be a way to explicitly dereference in PHP
Submitted: 2010-03-18 22:22 UTC Modified: 2015-08-05 19:39 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: niksoft at gmail dot com Assigned: cmb (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.3.2 OS: Linux (though it doesnt matter)
Private report: No CVE-ID: None
 [2010-03-18 22:22 UTC] niksoft at gmail dot com
Description:
------------
It seems that the part of PHP that is responsible for minimizing the footprint of passing around data, at least within an object, does too good of a job, and the lack of an explicit "dereference" or a forced, perhaps recursively-acting "assign by value" operator creates an issue where a programmer can not explicitly dereference the data behind that referenced value, that, for example, is being pushed into an array, would lead to an array of referenced values (with vales that would not be what you would predict them to be)

So the code: MyResult is a class meant to unify access to mysqli result data access between query and prepared statement. SQL is just a couple of commands to set up the testing environment (test table). The MyTest opens a connection and tries to get the data. Query results, which are passes back the array returned from mysqli's result fetch_array function are properly dereferenced in getArray method. The prepared statement results that are passed back as $this->bind_arr are not properly dereferenced, and get added to $ret as references, resulting in the final array that is filled with both id and value filled with the latest data set in the $this->bind_arr, which leads me to believe that this is a dereferencing issue, in this case id=3 and value=val3. If you add a print_r line to the while loop, you will see that with stored procedure results, every new result is passed back, all the values are updated to that result set.

Hopefully this is short enough to describe the issue,

-- Alex

Test script:
---------------
MyResult.php
http://pastebin.com/qJRWySks

SQL:
create table test_table (id int not null auto_increment primary key, value varchar(8));
insert into test_table (`value`) values ("val1"), ("val2"), ("val3");

MyTest.php
http://pastebin.com/AV6B79Vk

Expected result:
----------------
Prepared statement
Array ( [0] => Array ( [id] => 1 [value] => val1 ) [1] => Array ( [id] => 2 [value] => val2 ) [2] => Array ( [id] => 3 [value] => val3 ) [3] => 

Query
Array ( [0] => Array ( [id] => 1 [value] => val1 ) [1] => Array ( [id] => 2 [value] => val2 ) [2] => Array ( [id] => 3 [value] => val3 ) [3] => 

Actual result:
--------------
Prepared statement
Array ( [0] => Array ( [id] => 3 [value] => val3 ) [1] => Array ( [id] => 3 [value] => val3 ) [2] => Array ( [id] => 3 [value] => val3 ) [3] => )

Query
Array ( [0] => Array ( [id] => 1 [value] => val1 ) [1] => Array ( [id] => 2 [value] => val2 ) [2] => Array ( [id] => 3 [value] => val3 ) [3] => 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-08-05 19:39 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2015-08-05 19:39 UTC] cmb@php.net
There is no need for explicit "dereferencing", because there is no
need for passing references to a function (except, maybe, for
output parameters) as of PHP 5.0.0. So don't declare the parameter
of MyResult::__construct() as reference.

See also
<http://schlueters.de/blog/archives/125-Do-not-use-PHP-references.html>.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 27 13:01:30 2024 UTC