reference to a function

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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):

    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!


Re: reference to a function

Jeff wrote:

Quoted text here. Click to load it

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

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

El hijo de Bill Gates fue por causa de un 'ERROR DE PROTECCION GENERAL'.

Re: reference to a function

Quoted text here. Click to load it

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:

Quoted text here. Click to load it

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.

Quoted text here. Click to load it

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).


Site Timeline