2 rcs_id('$Id: WikiCallback.php,v 1.3 2004-11-01 10:43:56 rurban Exp $');
7 * This is a virtual class.
9 * Subclases of WikiCallback can be used to represent either
10 * global function callbacks, or object method callbacks.
12 * @see WikiFunctionCb, WikiMethodCb.
17 * Convert from Pear-style callback specification to a WikiCallback.
19 * This is a static member function.
21 * @param $pearCb mixed
22 * For a global function callback, $pearCb should be a string containing
23 * the name of the function.
24 * For an object method callback, $pearCb should be a array of length two:
25 * the first element should contain (a reference to) the object, the second
26 * element should be a string containing the name of the method to be invoked.
27 * @return object Returns the appropriate subclass of WikiCallback.
30 function callback ($pearCb) {
31 if (is_string($pearCb))
32 return new WikiFunctionCb($pearCb);
33 else if (is_array($pearCb)) {
34 list($object, $method) = $handler;
35 return new WikiMethodCb($object, $method);
37 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.
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.
60 function call_array ($args) {
61 trigger_error('pure virtual', E_USER_ERROR);
65 * Convert to Pear callback.
67 * @return string The name of the callback function.
68 * (This value is suitable for passing as the callback parameter
69 * to a number of different Pear functions and methods.)
73 trigger_error('pure virtual', E_USER_ERROR);
78 * Global function callback.
86 * @param $functionName string Name of global function to call.
89 function WikiFunctionCb ($functionName) {
90 $this->functionName = $functionName;
93 function call_array ($args) {
94 return call_user_func_array($this->functionName, $args);
98 return $this->functionName;
103 * Object Method Callback.
111 * @param $object object Object on which to invoke method.
112 * @param $methodName string Name of method to call.
115 function WikiMethodCb(&$object, $methodName) {
116 $this->object = &$object;
117 $this->methodName = $methodName;
120 function call_array ($args) {
121 $method = &$this->methodName;
122 //$obj = &$this->object;
124 // This should work, except PHP's before 4.0.5 (which includes mine)
125 // don't have 'call_user_method_array'.
126 if (check_php_version(4,0,5)) {
127 return call_user_func_array(array(&$this->object, $method), $args);
130 // This should work, but doesn't. At least in my PHP, the object seems
131 // to get passed by value, rather than reference, so any changes to the
132 // object made by the called method get lost.
134 switch (count($args)) {
135 case 0: return call_user_method($method, $obj);
136 case 1: return call_user_method($method, $obj, $args[0]);
137 case 2: return call_user_method($method, $obj, $args[0], $args[1]);
138 case 3: return call_user_method($method, $obj, $args[0], $args[1], $args[2]);
139 case 4: return call_user_method($method, $obj, $args[0], $args[1], $args[2], $args[3]);
140 default: trigger_error("Too many arguments to method callback", E_USER_ERROR);
144 // This seems to work, at least for me (so far):
145 switch (count($args)) {
146 case 0: return $this->object->$method();
147 case 1: return $this->object->$method($args[0]);
148 case 2: return $this->object->$method($args[0], $args[1]);
149 case 3: return $this->object->$method($args[0], $args[1], $args[2]);
150 case 4: return $this->object->$method($args[0], $args[1], $args[2], $args[3]);
151 default: trigger_error("Too many arguments to method callback", E_USER_ERROR);
155 function toPearCb() {
156 return array($this->object, $this->methodName);
161 * Anonymous function callback.
163 class WikiAnonymousCb
169 * @param $args string Argument declarations
170 * @param $code string Function body
171 * @see create_function().
174 function WikiAnonymousCb ($args, $code) {
175 $this->function = create_function($args, $code);
178 function call_array ($args) {
179 return call_user_func_array($this->function, $args);
182 function toPearCb() {
183 trigger_error("Can't convert WikiAnonymousCb to Pear callback",
188 // (c-file-style: "gnu")
193 // c-hanging-comment-ender-p: nil
194 // indent-tabs-mode: nil