LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   php: array with functions (https://www.linuxquestions.org/questions/programming-9/php-array-with-functions-233957/)

ldp 09-22-2004 03:39 PM

php: array with functions
 
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

ldp 09-22-2004 03:44 PM

I also tried changing the varia function to this:

public function varia($i, $x) {
if ($i == 0 or $i == 1) {
echo "you asked for $i <br />";
$f = $this->functList[$i];
echo "func: $f";
$this->$f($x);
//$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;
}
}

But that don't work either, in the output it seems that the array funcList never gets filled with the elements that I put:
$funcList = array('dealXdmg', 'getXlife');
that's the correct way to create an array, right?

Proud 09-22-2004 03:46 PM

Why are you attempting to make an array of functions?? What do you really want this piece of code to do and where gave you the idea that you could put functions in an array?? I've never heard of any language where you can do such a thing and cant think why you'd want to. Functions have local variables and are small sections of executable code, and you wish to store them in an array as data?? How do you plan to retrieve and execute them??

*baffled*

ldp 09-22-2004 04:02 PM

héhé, I was suprised too but php seems to be able to do it.

from php.net documentation


Variable functions
PHP supports the concept of variable functions. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callbacks, function tables, and so forth. :cool:

That's exactly what I want for my program.
I have a class Card that can have many functions.
If I have to define them all in that class, making the instances will eat my resources so I tought, with this I can just add the functions to that class that I really need for the instance that I create.

I also read somewhere on that site (but can't find it back :( ) that you can store code (functions) in a mysql record and use it in the program

Proud 09-22-2004 04:11 PM

Cursed funky PHP, this is not going to help with my choice between stugying C++ and messing with LAMP. :D

I'd seen variable variables but these variable functions take the cake-you could probably write an interactive development environment/shell like python has with this!
I dunno if your many functions are really going to compare to calling a database and then interpreting the return text at run time. I would have though having the functions in the one script would be able to be more optimized, but meh, we'll try this way. :)

Gimmie a sec to swat up on it. ;)

ldp 09-22-2004 04:18 PM

Powerfull stuff I'd say. :cool:
Also did some C++ some time ago but it's more complex and much stricter than php.
In Java, I never heard about variable functions either.

In the end, I can define all the functions in the class indeed but if this array of functions is possible, I would like to see it work. :)

Thanks for the help already, I keep searching too. (but will need some sleep soon)

btw: the game is in fact "Magic" Most people seem at least to know it. :)
So I'll create the instances (about 130 cards in 2 decks in 1 game) only once and thus need only one time to open the db and create the instances upon starting the game and afterwards, db gets closed and game can start.
Sounded nice to me. (but that's me eh... )

Lieven

Proud 09-22-2004 04:38 PM

Ok, if you have a variable which holds a string value, and you have the line variablename() PHP will attempt to find the function with the same name as the string variablename contains. Any arguments you wish to pass to the function go within the paranthesis, (), as usual.

Where this fails to help you out is that the function has to already be defined, so must already be in the code you've executed. There's the create_function() function which seems to use variable functions to allow you to call an otherwise anonymous, user defined function. Sounds like more execution overhead.

What you're actually doing is initialising a lot of objects of your card class, and I'm guessing by some testing of a name string or something are determining which functions that card will require ingame. I think you'll find you need to look at inheritence, and to define a base card class with the functions that all card objects will need, and then use child classes with more/redefined functions for different cases of card object you're creating.
I think the whole string<-database->interpret function name->call function isn't going to save you resources or effort.
Ultimately you want a lot of cards, but each instance of your card object might actually have differing sets of functions defined to it which it is valid to call/use?

ldp 09-22-2004 04:55 PM

Indeed, at first I also tought about inheritance, it's clear that Card is in fact some abstract class because all cards are of a certain 'type' and Card don't exist on it's own. And it's the most logic thing to do.
But this variable function tables intrigued me very much. And also the fact that php seems to be able to interpret data as actual code sounded very interesting to me.
I'll continue with the inheritance solution for now and will keep looking for the other solution. If it exists, it will turn up somewhere, sometime. :D

Anyway, thanks for the help, it's appreciated.
But now I'll need my sleep so I'll be able to think clear again tomorrow.

Lieven


All times are GMT -5. The time now is 10:40 PM.