View Full Version : [SOLVED] PHP class question

May 2nd, 2012, 12:16 AM
Trying to figure out how to grab an element from an array variable inside a class, where the array variable is labeled private. Does anyone know what I'm doing wrong?


class GetArray {

private var $a;

function __construct(){
$this->a = array_fill(0, 10, 1.0);
for($i = 0; $i < 10; $i++){
$this->a[$i] = $i;

public function getA(){
return $this->a;


$new_class = new GetArray();

echo $new_class->getA()[4]."\n"; //ERROR HERE


May 2nd, 2012, 02:34 PM
Array dereferencing is new since PHP 5.4.0, so unless you're running 5.4.0+ then you'll get a syntax error. To work around it, simply assign the return value to a variable, and then access the array index from that variable.

May 3rd, 2012, 05:02 PM
Ah, thanks. That works.