Re: dynamic/multidimensional arrays in classes
| From: | James Holden | Date: | Mon, 10 Mar 2003 10:01:26 +0000 |
| Subject: | Re: dynamic/multidimensional arrays in classes | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-138939@lists.php.net to get a copy of this message | ||
$this->$arr[]
should be
$this->arr[]
You are using a variable defined within the class - since it is a class variable you can reference it as you would anything else within the class, using '$this->'
Patrick Teague wrote:
I'm having problems figuring this out. This first way gives me a 'Fatal Error: Cannot use [] for reading in class.php on line xx' class myClass { var $arr = array(); function add_something( $value ) {$this->$arr[] = $value; // this is the line causing the error} } I've also tried using count() to find out how many items are in $arr, but it keeps saying that $r == 0... i.e. function add_something( $value ) { $r = count($arr); /* if( is_null( $r ) {$r = 0;} */ //print( $r ); $this->$arr[$r] = $value; } I've tried this both with & without the commented section & still $r = 0 even if you use '$class->add_something("my value");' 50 times. I'm guessing once this problem is solved it will work for multidimensional arrays as well? Patrick