6 * This is a virtual class.
8 * Subclases of WikiCallback can be used to represent either
9 * global function callbacks, or object method callbacks.
11 * @see WikiFunctionCb, WikiMethodCb.
13 abstract class WikiCallback
16 * Convert from Pear-style callback specification to a WikiCallback.
18 * This is a static member function.
20 * @param $pearCb mixed
21 * For a global function callback, $pearCb should be a string containing
22 * the name of the function.
23 * For an object method callback, $pearCb should be a array of length two:
24 * the first element should contain (a reference to) the object, the second
25 * element should be a string containing the name of the method to be invoked.
26 * @return object Returns the appropriate subclass of WikiCallback.
28 public function callback($pearCb)
30 if (is_string($pearCb))
31 return new WikiFunctionCb($pearCb);
32 else if (is_array($pearCb)) {
33 list($object, $method) = $handler;
34 return new WikiMethodCb($object, $method);
36 trigger_error("WikiCallback::new: bad arg", E_USER_ERROR);
43 * @param ? mixed This method takes a variable number of arguments (zero or more).
44 * The callback function is called with the specified arguments.
45 * @return mixed The return value of the callback.
47 public function call()
49 return $this->call_array(func_get_args());
53 * Call callback (with args in array).
55 * @param $args array Contains the arguments to be passed to the callback.
56 * @return mixed The return value of the callback.
57 * @see call_user_func_array.
59 abstract public function call_array($args);
62 * Convert to Pear callback.
64 * @return string The name of the callback function.
65 * (This value is suitable for passing as the callback parameter
66 * to a number of different Pear functions and methods.)
68 abstract public function toPearCb();
72 * Global function callback.
78 * @param $functionName string Name of global function to call.
80 public function __construct($functionName)
82 $this->functionName = $functionName;
85 function call_array($args)
87 return call_user_func_array($this->functionName, $args);
92 return $this->functionName;
97 * Object Method Callback.
103 * @param $object object Object on which to invoke method.
104 * @param $methodName string Name of method to call.
106 function __construct(&$object, $methodName)
108 $this->object = &$object;
109 $this->methodName = $methodName;
112 function call_array($args)
114 $method = &$this->methodName;
115 return call_user_func_array(array(&$this->object, $method), $args);
120 return array($this->object, $this->methodName);
125 * Anonymous function callback.
127 class WikiAnonymousCb
131 * @param $args string Argument declarations
132 * @param $code string Function body
133 * @see create_function().
135 function __construct($args, $code)
137 $this->function = create_function($args, $code);
140 function call_array($args)
142 return call_user_func_array($this->function, $args);
147 trigger_error("Can't convert WikiAnonymousCb to Pear callback",
156 // c-hanging-comment-ender-p: nil
157 // indent-tabs-mode: nil