#27776 [Opn]: arrays in class behave differently than expected

From: Date: Tue, 30 Mar 2004 13:39:10 +0000
Subject: #27776 [Opn]: arrays in class behave differently than expected
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-57109@lists.php.net to get a copy of this message
 ID:               27776
 User updated by:  pedja at uzice dot net
 Reported By:      pedja at uzice dot net
 Status:           Open
 Bug Type:         Arrays related
 Operating System: Windows XP
 PHP Version:      4.3.4
 New Comment:

Hmm... i found out what was the problem.

Instead of $this->$testarray[$counter] i should use
$this->testarray[$counter]


Previous Comments:
------------------------------------------------------------------------

[2004-03-30 08:32:14] pedja at uzice dot net

Description:
------------
I created array as class property. It does not work as expected. Here
is an example:

<?php

  class testclass { 
    var $testarray = array();
  
    function testclass() {
      for ($counter = 65; $counter < 75; $counter++) {
        $this->$testarray[$counter] = chr ($counter);
      }
    }
    
  }
  
  $testvar = new testclass();
  print_r ($testvar->$testarray);

?>

The result of this code is array containing one record. It should
contain several records.

If I cahnge this code to use global variable instead of class property
... 

<?php

  class testclass { 
  
    function testclass() {
      global $testarray;
      
      for ($counter = 65; $counter < 75; $counter++) {
        $testarray[$counter] = chr ($counter);
      }
    }
    
  }
  
  $testvar = new testclass();
  print_r ($testarray);

?>

... it works: I get what I expected - array containing several
records.




------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=27776&edit=1


Thread (4 messages)

« previous php.bugs (#57109) next »