By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,751 Members | 1,158 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,751 IT Pros & Developers. It's quick & easy.

reference to a function

P: n/a
I'm porting a bit of perl to php and am emulating the perl hash table. I
asked about this before, but I've since lost my way.

What I have is this (in a class):

if(function_exists($CUSTOM['some_key'])){
return $CUSTOM['some_key']($this,$some_param);
}

Now, I can do this:

$CUSTOM['some_key'] = create_function('$obj,$someparam','return
"something done here";');

What I'd like to do is point this at a function in a class elsewhere,
in pseudo code:

$CUSTOM['some_key'] = {
require_once ('some_module');
function_in_some_module($obj, $some_param);
};

I have some notion that this can be done with: call_user_func_array,
but I can't quite grok the syntax. Help!

Jeff
Sep 19 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Jeff wrote:
$CUSTOM['some_key'] = {
require_once ('some_module');
function_in_some_module($obj, $some_param);
};
Providing that you're calling create_function() in the 'some_module' file,
you *only* have to include/require the file - $CUSTOM['some_key'] will be
filled up with the lambda function. Keep in mind that as of PHP5, variable
scope is kept the same across included files (this will change with PHP6's
namespaces).

--
----------------------------------
Iván Sánchez Ortega -ivan-algarroba-sanchezortega-punto-es-

El hijo de Bill Gates fue por causa de un 'ERROR DE PROTECCION GENERAL'.
Sep 19 '08 #2

P: n/a
On 19 Sep, 21:24, Jeff <jeff@spam_me_not.comwrote:
I'm porting a bit of perl to php and am emulating the perl hash table. I
asked about this before, but I've since lost my way.

What I have is this (in a class):

if(function_exists($CUSTOM['some_key'])){
return $CUSTOM['some_key']($this,$some_param);
}

Now, I can do this:

$CUSTOM['some_key'] = create_function('$obj,$someparam','return
"something done here";');

What I'd like to do is point this at a function in a class elsewhere,
in pseudo code:

$CUSTOM['some_key'] = {
require_once ('some_module');
function_in_some_module($obj, $some_param);

};

I have some notion that this can be done with: call_user_func_array,
but I can't quite grok the syntax. Help!

Jeff
I know its confusing asking for something when you don't know what it
is your asking for - but I got a bit lost in your post:
What I'd like to do is point this at a function in a class elsewhere,
Do you mean a class as in OO? In that case its a method - not a
function. And do you need to access private/protected properties? Or
do you just want to encapsulate an existing method call or create a
new method?

It's hard to make calls on how apposite your solution is without
knowing more about the context in which it will execute.
$CUSTOM['some_key'] = create_function('$obj,$someparam','return
"something done here";');
If you don't know at design time what the function will be - that
rather implies you don't know where it will be. It also begs the
question whether assigning $CUSTOM['some_key'] will be done once or if
it will be reasigned, and conversely given some function, will it be
used by multiple keys? Will all function use the same number and types
of parameters?

Also, optimally, you want to reduce the amount of code implemented
with 'create_function' or 'eval' - as this cannot be pre-tokenized/
cached by an accelerator. (If performance is important).

C.
Sep 20 '08 #3

P: n/a
C. (http://symcbean.blogspot.com/) wrote:
On 19 Sep, 21:24, Jeff <jeff@spam_me_not.comwrote:
>I'm porting a bit of perl to php and am emulating the perl hash table. I
asked about this before, but I've since lost my way.

What I have is this (in a class):

if(function_exists($CUSTOM['some_key'])){
return $CUSTOM['some_key']($this,$some_param);
}

Now, I can do this:

$CUSTOM['some_key'] = create_function('$obj,$someparam','return
"something done here";');

What I'd like to do is point this at a function in a class elsewhere,
in pseudo code:

$CUSTOM['some_key'] = {
require_once ('some_module');
function_in_some_module($obj, $some_param);

};

I have some notion that this can be done with: call_user_func_array,
but I can't quite grok the syntax. Help!

Jeff

I know its confusing asking for something when you don't know what it
is your asking for - but I got a bit lost in your post:
> What I'd like to do is point this at a function in a class elsewhere,

Do you mean a class as in OO? In that case its a method - not a
function. And do you need to access private/protected properties? Or
do you just want to encapsulate an existing method call or create a
new method?

It's hard to make calls on how apposite your solution is without
knowing more about the context in which it will execute.
I've worked this out, I had a syntax problem (and a missing trailing
semicolon) and got confused by the examples. All I needed was this:

$CUSTOM['some_key'] =
create_function('$a,$b','require_once("path_to_som e_module.php");return
getMyFunction($a,$b);');

Just to let you know what I'm doing:

It's all part of what I think you would call a templating engine. If
I write a call to some_key in the template, it looks to see if a
function has been defined for that and returns that.

I suppose there are many ways this can be done. But this way I can
keep the instructions in the template, and the directions to the
instructions in a common file, and then the actual instructions could be
anywhere so I can write libraries I can reuse.
>
>$CUSTOM['some_key'] = create_function('$obj,$someparam','return
"something done here";');

If you don't know at design time what the function will be - that
rather implies you don't know where it will be. It also begs the
question whether assigning $CUSTOM['some_key'] will be done once or if
it will be reasigned, and conversely given some function, will it be
used by multiple keys? Will all function use the same number and types
of parameters?
Yes. "$a" is a hash of the page environment (where it is, what it...)
and "$b" carries the full set of the instruction from the template

For example, here's a tag that loads a tiny mce editor:

<edit mode="editor" name="edit1" editor_class="master" rows="10"
header="0" style="width:480px" />

and for a custom side navigation (in this case context sensitive
depending on what section/subsection in the site)

<edit mode="custom" name="side_nav" />

$a already contains all that path info., $b contains a hash of the tag
params. The "name" is the key here: $CUSTOM['side_nav']
>
Also, optimally, you want to reduce the amount of code implemented
with 'create_function' or 'eval' - as this cannot be pre-tokenized/
cached by an accelerator. (If performance is important).
I couldn't figure out how to do this with: call_user_func_array, but
then since most of the pages are static and just created when they need
to be changed, performance is not a big issue.

Thanks. There are probably better ways of doing this, but this is the
way I've done it in perl. And for me, I'm not reinventing my wheel.

Jeff
>
C.
Sep 20 '08 #4

P: n/a
On 20 Sep, 17:56, Jeff <jeff@spam_me_not.comwrote:
C. (http://symcbean.blogspot.com/) wrote:
On 19 Sep, 21:24, Jeff <jeff@spam_me_not.comwrote:
I'm porting a bit of perl to php and am emulating the perl hash table. I
asked about this before, but I've since lost my way.
What I have is this (in a class):
if(function_exists($CUSTOM['some_key'])){
return $CUSTOM['some_key']($this,$some_param);
}
Now, I can do this:
$CUSTOM['some_key'] = create_function('$obj,$someparam','return
"something done here";');
What I'd like to do is point this at a function in a class elsewhere,
in pseudo code:
$CUSTOM['some_key'] = {
require_once ('some_module');
function_in_some_module($obj, $some_param);
};
I have some notion that this can be done with: call_user_func_array,
but I can't quite grok the syntax. Help!
Jeff
I know its confusing asking for something when you don't know what it
is your asking for - but I got a bit lost in your post:
What I'd like to do is point this at a function in a class elsewhere,
Do you mean a class as in OO? In that case its a method - not a
function. And do you need to access private/protected properties? Or
do you just want to encapsulate an existing method call or create a
new method?
It's hard to make calls on how apposite your solution is without
knowing more about the context in which it will execute.

I've worked this out, I had a syntax problem (and a missing trailing
semicolon) and got confused by the examples. All I needed was this:

$CUSTOM['some_key'] =
create_function('$a,$b','require_once("path_to_som e_module.php");return
getMyFunction($a,$b);');

Just to let you know what I'm doing:

It's all part of what I think you would call a templating engine. If
I write a call to some_key in the template, it looks to see if a
function has been defined for that and returns that.
OK - but wouldn't it just be simpler to:

function my_function_finder($fn)
{
if (function_exists($fn)) return true;
....// attempt to load lib
return function_exists($fn);
}

$result=my_function_finder($CUSTOM[$some_key]) ?
$CUSTOM[$some_key]($a,$b) :
false;
Also, optimally, you want to reduce the amount of code implemented
with 'create_function' or 'eval' - as this cannot be pre-tokenized/
cached by an accelerator. (If performance is important).

I couldn't figure out how to do this with: call_user_func_array, but
then since most of the pages are static and just created when they need
to be changed, performance is not a big issue.
call_user_func_array() is just a way to a call an existing user
function using an array instead of a list of arguments.
(user functions are the ones coded in PHP rather than the builtin PHP
functions or just the ones created at runtime).

C.
Sep 22 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.