reference to a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff

    reference to a function

    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_exi sts($CUSTOM['some_key'])){
    return $CUSTOM['some_key']($this,$some_pa ram);
    }

    Now, I can do this:

    $CUSTOM['some_key'] = create_function ('$obj,$somepar am','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_som e_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
  • =?UTF-8?B?SXbDoW4gU8OhbmNoZXogT3J0ZWdh?=

    #2
    Re: reference to a function

    Jeff wrote:
    $CUSTOM['some_key'] = {
    require_once ('some_module') ;
    function_in_som e_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'.

    Comment

    • C. (http://symcbean.blogspot.com/)

      #3
      Re: reference to a function

      On 19 Sep, 21:24, Jeff <jeff@spam_me_n ot.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_exi sts($CUSTOM['some_key'])){
      return $CUSTOM['some_key']($this,$some_pa ram);
      }
      >
      Now, I can do this:
      >
      $CUSTOM['some_key'] = create_function ('$obj,$somepar am','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_som e_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,$somepar am','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_functio n' or 'eval' - as this cannot be pre-tokenized/
      cached by an accelerator. (If performance is important).

      C.

      Comment

      • Jeff

        #4
        Re: reference to a function

        C. (http://symcbean.blogspot.com/) wrote:
        On 19 Sep, 21:24, Jeff <jeff@spam_me_n ot.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_exi sts($CUSTOM['some_key'])){
        > return $CUSTOM['some_key']($this,$some_pa ram);
        > }
        >>
        > Now, I can do this:
        >>
        >$CUSTOM['some_key'] = create_function ('$obj,$somepar am','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_som e_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','requi re_once("path_t o_some_module.p hp");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,$somepar am','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="m aster" rows="10"
        header="0" style="width:48 0px" />

        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_functio n' 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.

        Comment

        • C. (http://symcbean.blogspot.com/)

          #5
          Re: reference to a function

          On 20 Sep, 17:56, Jeff <jeff@spam_me_n ot.comwrote:
          C. (http://symcbean.blogspot.com/) wrote:
          On 19 Sep, 21:24, Jeff <jeff@spam_me_n ot.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_exi sts($CUSTOM['some_key'])){
          return $CUSTOM['some_key']($this,$some_pa ram);
          }
          >
          Now, I can do this:
          >
          $CUSTOM['some_key'] = create_function ('$obj,$somepar am','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_som e_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','requi re_once("path_t o_some_module.p hp");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_fin der($fn)
          {
          if (function_exist s($fn)) return true;
          ....// attempt to load lib
          return function_exists ($fn);
          }

          $result=my_func tion_finder($CU STOM[$some_key]) ?
          $CUSTOM[$some_key]($a,$b) :
          false;
          Also, optimally, you want to reduce the amount of code implemented
          with 'create_functio n' 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.

          Comment

          Working...