Req #51327 [Opn->Csd]: Perhaps there should be a way to explicitly dereference in PHP
Edit report at https://bugs.php.net/bug.php?id=51327&edit=1
ID: 51327
Updated by: cmb@php.net
Reported by: niksoft at gmail dot com
Summary: Perhaps there should be a way to explicitly
dereference in PHP
-Status: Open
+Status: Closed
Type: Feature/Change Request
Package: Scripting Engine problem
Operating System: Linux (though it doesnt matter)
PHP Version: 5.3.2
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
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>.
Previous Comments:
------------------------------------------------------------------------
[2010-03-18 22:22:22] 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] =>
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=51327&edit=1
Thread (2 messages)