0

我需要生成函数名然后调用它。是否可以在 php 中做类似的事情 <?php call_user_func_array(array($object, $method));?>

4

1 回答 1

2

There are four options:

  1. Make the methods you want to call like this signals. Signals can be emited by name GLib.Signal.emit_by_name (g_signal_emit_by_name). The call is from GLib mode, but other modes with signal support are likely to have similar method.

  2. Create a static table/hash table of delegate objects manually in code. The main advantage is that it is type-safe. Disadvantage is that you have to add each method in two places. It will also work in all vala modes.

  3. Another option is to tell vala compiler to build the "gir" binding and use the GObject Introspection library to call the functions. That is much more complicated, but the compiler will maintain the list of available methods for you. This method is specific to the GLib mode.

  4. The last option is to use the GLib.Module.symbol (g_module_symbol) function of GLib to find the symbol. You'll need to know the "mangled" C name of the symbol and it will not be type-safe. You will have to match argument types exactly and mind where the invocant should go. It avoids the overhead of GIR, but unlike GIR it can't tell you which methods exist, only get you a specific one. This method is used when connecting signals in GtkBuilder. I mentioned the function from GLib, but POSIX.dlsym can be used the same way.

于 2012-09-14T08:33:43.677 回答