Hi,
I try to make an array of functions with php.
The ultimate goal is to make a class which creates an array full of functions. These functions should be dynamically loaded at creation of an instance of the class and come from a mysql db.
I don't have problems with mysql, know how to connect to db via php and execute queries.
For starters (I have to start somewhere with this) I have this:
<?php
/* test to check if function tables are possible. */
class funcTable
{
private $funcList;
private $i;
public function __construct() {
$i = 0;
echo "constructing... <br />";
$funcList = array('dealXdmg', 'getXlife');
}
public function dealXdmg($x) {
echo "deal $x dmg <br />";
return true;
}
public function getXlife($x) {
echo "get $x life <br />";
return true;
}
public function varia($i, $x) {
if ($i == 0 or $i == 1) {
echo "you asked for $i <br />";
$this->funcList[$i]($x);
// don't work with $this->$funcList[$i]($x) either.
//echo "<br/>varia: " . $this->$funcList[$i] . "<br />";
return true;
} else {
echo "expecting a 0 or 1 <br />";
return false;
}
}
}
echo "test functable 01: <br />";
$tst01 = new funcTable;
$fname = "dealXdmg";
$tst01->$fname(20);
echo "<br />test functable 02: <br />";
$tst02 = new funcTable;
$tst02->varia(1, 20);
?>
most part of it seem to work, at least, there is no syntax error because my php parser shows some of the output.
but the following line is of my concern:
$this->funcList[$i]($x);
I also tried $this->$funcList[$i]($x);
And now I'm wondering if the fault is in $funcList with or without $ sign
Or maybe the fault is that I can't do this: array[elementX](parameters);
How do I correctly pass parameters to a function in an array?
this don't work either: $this->$funcList[$i($x)];
a working example of a function list would be most welcome ofcourse
thanks,
Lieven