I’m starting to learn php and am using a maze generator to test my knowledge. Each vertex is a member of class Grid in a 2D array ($gridlist[$x][$y])
There is a class variable for each direction (NSEW) and for ‘visited’. A constructor sets all variables to 0. I’ve included a function in the class to set the connectors between vertices, so calling $gridlist[2][2]->set_connect(“N”) will set the Nconnect variable to 1.
To connect two points together, each side of the connection needs to be set, so I wrote a function (not in the class) to do this:
function connect_twoV2($a, $b, $d){
$c = $gridlist[$a][$b];
switch ($d){
case "N":
$c->set_connect("N");
$e = $gridlist[$a][$b-1];
$e->set_connect("S");
break;
case "S":
$c->set_connect("S");
$e = $gridlist[$a][$b+1];
$e->set_connect("N");
break;
case "E":
$c->set_connect("E"); ///This is line 170
$e = $gridlist[$a+1][$b];
$e->set_connect("W");
break;
case "W":
$c->set_connect("W");
$e = $gridlist[$a-1][$b];
$e->set_connect("E");
break;
}
}
However, when I run the file, which contains this call:
connect_twoV2(1, 2, "E");
I get:
Fatal error: Call to a member function set_connect() on a non-object in C:\XAMPP\xampplite\htdocs\phptest\zMaze3.php on line 170
Can anyone tell me why the class function call doesn’t work on $c?
Many thanks
ht
(php 5.3.1 on xampp 1.7.3, testing with IE8)
$gridlistis a variable declared outside of your function and therefor is not accessible from inside of it. You can solve this one of two ways:1) Use the (evil)
globalkeyword:2) Pass it as a parameter to your method (preferred method)