php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #12692 The current() function does not return references
Submitted: 2001-08-10 13:21 UTC Modified: 2001-08-10 15:57 UTC
From: metz at studenten dot net Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.0.6 OS: Linux 2.4.2
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: metz at studenten dot net
New email:
PHP Version: OS:

 

 [2001-08-10 13:21 UTC] metz at studenten dot net
When filling an array with reference to objects
the current() function does not return the reference to the
object but a copy of the object.

i.e.
   $ref = &current($some_array);
returns a copy

work around:
   $ref = &$some_array[key($some_array)];
returns the reference

Is this a bug or a difference in implementation?

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-08-10 13:27 UTC] metz at studenten dot net
test script:

    // Class definiton used for reference testing
    class Test
    {
        var $t = "x";

        function Test(){}

        function get_t()
        {
            return($this->t);
        }

        function set_t($t)
        {
            $this->t = $t;
        }
    }

    // Array container reference test

    $ref_one       = new Test();
    $ref_two       = new Test();
    $ref_three     = new Test();

    $ref_one->t    = "one";
    $ref_two->t    = "two";
    $ref_three->t  = "three";

    $refs          = array();
    $refs["one"]   = &$ref_one;
    $refs["two"]   = &$ref_two;
    $refs["three"] = &$ref_three;

    print("<B>Test 7-1: (start)</B><BR>\n");
    reset($refs);
    for($idx = 0; $idx < count($refs); $idx++)
    {
        $key = key($refs);
        $tmp = &$refs[$key];

        print("${key} => " . $tmp->get_t() . "<BR>\n");
        next($refs);
    }
    print("<BR>\n");

    print("<B>Test 7-2: (using &amp;current() to
adjust)</B><BR>\n");
    reset($refs);
    for($idx = 0; $idx < count($refs); $idx++)
    {
        $key = key($refs);
        $tmp = &current($refs);

        print("Setting: ${key} => ${idx}<BR>\n");
        $tmp->set_t($idx);
        next($refs);
    }
    reset($refs);

    print("<B>Result:</B><BR>\n");
    for($idx = 0; $idx < count($refs); $idx++)
    {
        $key = key($refs);
        $tmp = &$refs[$key];

        print("${key} => " . $tmp->get_t() . "<BR>\n");
        next($refs);
    }
    print("<BR>\n");

    print("<B>Test 7-3: (using direct method to
adjust)</B><BR>\n");
    reset($refs);
    for($idx = 0; $idx < count($refs); $idx++)
    {
        $key = key($refs);
        $tmp = &$refs[$key];

        print("Setting: ${key} => ${idx}<BR>\n");
        $tmp->set_t($idx);
        next($refs);
    }
    reset($refs);

    print("<B>Result:</B><BR>\n");
    for($idx = 0; $idx < count($refs); $idx++)
    {
        $key = key($refs);
        $tmp = &$refs[$key];

        print("${key} => " . $tmp->get_t() . "<BR>\n");
        next($refs);
    }
    print("<BR>\n");
 [2001-08-10 15:57 UTC] jeroen@php.net
That's intentional
 [2004-03-27 07:38 UTC] bugs dot php dot net-2004 dot 03 dot 27 at m-me dot dk
Any idea if this is ever gonna be fixed?
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jan 15 12:01:29 2025 UTC