LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-22-2004, 03:39 PM   #1
ldp
Member
 
Registered: Apr 2004
Location: Belgium Antwerpen
Distribution: slackware - knoppix
Posts: 141

Rep: Reputation: 18
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
 
Old 09-22-2004, 03:44 PM   #2
ldp
Member
 
Registered: Apr 2004
Location: Belgium Antwerpen
Distribution: slackware - knoppix
Posts: 141

Original Poster
Rep: Reputation: 18
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?
 
Old 09-22-2004, 03:46 PM   #3
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
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*
 
Old 09-22-2004, 04:02 PM   #4
ldp
Member
 
Registered: Apr 2004
Location: Belgium Antwerpen
Distribution: slackware - knoppix
Posts: 141

Original Poster
Rep: Reputation: 18
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.

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
 
Old 09-22-2004, 04:11 PM   #5
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

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

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.

Last edited by Proud; 09-22-2004 at 04:12 PM.
 
Old 09-22-2004, 04:18 PM   #6
ldp
Member
 
Registered: Apr 2004
Location: Belgium Antwerpen
Distribution: slackware - knoppix
Posts: 141

Original Poster
Rep: Reputation: 18
Powerfull stuff I'd say.
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

Last edited by ldp; 09-22-2004 at 04:24 PM.
 
Old 09-22-2004, 04:38 PM   #7
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
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?
 
Old 09-22-2004, 04:55 PM   #8
ldp
Member
 
Registered: Apr 2004
Location: Belgium Antwerpen
Distribution: slackware - knoppix
Posts: 141

Original Poster
Rep: Reputation: 18
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.

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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
using arrays and functions and trying to initialize a point in the array mshinska Programming 1 11-11-2005 02:21 AM
array functions in c djgerbavore Programming 6 01-07-2005 03:28 PM
PHP about functions djgerbavore Programming 1 11-25-2004 07:14 PM
functions in PHP gaddargarson Programming 3 10-07-2004 02:02 PM
PHP -- How to execute a shell script from PHP using FTP functions?? zoonalex Programming 3 07-29-2004 11:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration