php - Is an 'invoke'-style callback possible in PHP5? -


PHP functions such as 'array_map' take a callback, which can be a simple task or class or object method:

P>

  $ array2 = array_map ('myFunc', $ array);  

or

  $ array2 = array_map (array ($ object, 'myMethod'), $ array);  

But is there a syntax for passing this method that is bound within the current object that is running (such as 'prototype' js in 'avoc')? So that the following can be used:

  $ array2 = array_map ('myMethod', $ array);  
  foreach ($ array as $ $ obj) $ array2 [] = $ obj- & gt; With the effect of myMethod ()  

Obviously I can use this form, or I can write cover function to call I can, and even even inline. But since 'my mathith' is already a method, it seems that one of these has to be done.

  function obj_array_map ($ method, $ arr_of_objects) {$ out = array (); $ Args = array_slice (func_get_args (), 2); Forex Currency ($ arr_of_objects $ key = & gt; $ obj) {$ out [$ key] = call_user_func_array (array ($ obj, $ method), $ args); } Return Out $; } // This code is $ a = array ($ obj1, $ obj2); Obj_array_map ('method', $ a, 1, 2, 3); // Results in Call: $ obj1- & gt; Method (1, 2, 3); $ Obj2- & gt; Method (1, 2, 3);  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -