php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #12350 Referencing multiple arrays in classes from inside methods doesn't work
Submitted: 2001-07-24 16:13 UTC Modified: 2001-07-25 05:29 UTC
From: m dot stenzel at dragondezign dot de Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 4.0.6 OS: W2K
Private report: No CVE-ID: None
 [2001-07-24 16:13 UTC] m dot stenzel at dragondezign dot de
Configuration: IIS 4 dll with Win32 binaries

Okay I'm new to the game so forgive me a small mistake but I have tested and reproduced this weird behavior for more than 6 hours.

A class wrapping multiple array variables doesn't allow to access both arrays as individual references from inside a function.

The following code does not what I expected from the language:

class test {
  VAR $firstarray;  // defining one array
  VAR $secondarray; // defining another array
	
  function testit () {
    $i = 0;
    while ($i < 14) {
      $this->$firstarray[$i] = uniqid ("test", false);
	  $this->$secondarray[$i] = uniqid ("different", false);
      echo "Set array #1, item $i to ".$this->$firstarray[$i]." and array #2 to ".$this->$secondarray[$i]."\n";
	  $i++;
	}
  }

  function doitlocal () {
    $i = 0;
    while ($i < 14) {
      $firstarray[$i] = uniqid ("test", false);
	  $secondarray[$i] = uniqid ("different", false);
      echo "Set array #1, item $i to ".$firstarray[$i]." and array #2 to ".$secondarray[$i]."\n";
	  $i++;
	}
  }
  
}

  $temp = new test;
  $temp->testit();
  echo "The (wrong) result shows that both arrays contain the same data.\n\n";
  echo "Now lets do it local:\n";
  $temp->doitlocal();
  echo "With local variables everything works as expected.";

I expected the same result from both the testit() and doitlocal() methods and that would be creating two distinctive arrays and accessing them later in the script just with different values. This seems not to be possible if the array is capsulated in the class.

I think i read the manual thorougly and haven't found a hint about this behavior :(

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-07-25 05:29 UTC] cynic@php.net
use $this->firstarray, not $this->$firstarray
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 04:01:28 2024 UTC