/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Storing functions in a variable

Is it possible to store a function itself NOT the value that it returns in a variable?

I’m trying to create an array I can use to formulate insert statements, but I need to calculate the data for the insert statements. I don’t want to have to edit 3 places when I want to add a field or change a calculation.

Something like this:

[code=php]
$columns = array
(
[FIELD1] => CALCULATION / FUNCTION,
[FIELD2] => CALCULATION / FUNCTION
);
[/code]

Any ideas?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 03.2006 — It's not clear to me why you would need to do this, but assuming I'm just not grasping your intention, perhaps you could store the function names in the array:
[code=php]
$functions = array (
['f1'] => 'function_one',
['f2'] => 'function_two',
['f3'] => 'function_three'
);
[/code]

Then anywhere you need to use function_two(), for example:
[code=php]
call_user_func($functions['f2'], $param1, $param2);
[/code]

But as I said, it's not really clear to me what you want, so I may be totally off-base here.
Copy linkTweet thisAlerts:
@CWCJimmyauthorAug 04.2006 — Thanks for the response.

I apologize that wasn't a very clear post. Let me try to re-explain what I'm trying to do. I'm dumping data from an source via ODBC to a MySQL database. I had an array that contained the Fieldname it's datatype and an SQL statement to get it's value. Like so:
[code=php]
$mysql_table= array (
"TableName" => array (
"SKU" => array (
"Datatype" => "VARCHAR(20)",
"SQL" => "TRIM(ItemNumber)"),
"DESCP_1" => array (
"Datatype" => "VARCHAR(255)",
"SQL" => "TRIM(ItemDescription)"),
"IMG_EXIST" => array (
"Datatype" => "INT(15)",
"SQL" => "IF(ImageFile = '', 'N', 'Y')")
)
);
[/code]


This was used to create the table and insert statements for MySQL. This, however, will no longer work becuase the ODBC driver does not support all of the functions I need. So I was hoping to replace the SQL functions with functions in PHP so I could achieve the same result. Like so:

[code=php]
$mysql_table= array (
"TableName" => array (
"SKU" => array (
"Datatype" => "VARCHAR(20)",
"SQL" => trim($value)),
"DESCP_1" => array (
"Datatype" => "VARCHAR(255)",
"SQL" => trim($value)),
"IMG_EXIST" => array (
"Datatype" => "INT(15)",
"SQL" => empty($value) ? 'N' : 'Y'))
)
);
[/code]


But to do that I would need to store the function/calculation itself, and not just the result that the it returned becuase this array would be declared outside the function it is to be used in.

The whole reason I want to do it this way is because this way I can easily add/remove/edit fields by editing one place.

Hope that makes a bit more sense
Copy linkTweet thisAlerts:
@CWCJimmyauthorAug 09.2006 — Not to bring a dead topic back but I found what I was looking for and I figured it may help someone.

I stumbled across this [URL=http://us3.php.net/manual/en/function.create-function.php]create_function[/URL] function on the PHP website. You can literally write the syntax for the functions and have the syntax stored in a variable.

Pretty slick.
×

Success!

Help @CWCJimmy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...