...

View Full Version : oop problem



F-b0mb
03-17-2009, 11:48 PM
I am just learning oop so this may be obvious but I can't see what is wrong here.
class board {
function __construct($fen){
$ranks = array_reverse (explode("/", $fen));
}
function get_rank($rank){
return $this->ranks[$rank];
}
};

$grid = array(1=> new board('xx/xx/xx'), new board('xx/xx/xx'), new board('xx/xx/xx'));

echo $grid[1]->get_rank(1); //why does this return nothing?
Thanks in advance for any help.

Fou-Lu
03-18-2009, 12:52 AM
function __construct($fen){
$ranks = array_reverse (explode("/", $fen));
}


PHP doesn't use variable masking, so you have to use $this->ranks in order to store it. You should declare the variable though with private $ranks in order to prevent errors from you're accessor.

F-b0mb
03-18-2009, 01:13 AM
Thanks Fou, I knew it would be something obvious since I spent an hour or so trying to fix it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum