8.7 Declaring Functions Dynamically


You want to declare a function dynamically, depending on a certain condition.

Technique

PHP enables you to do so, although you probably will not use this feature much. It might come in handy if you expect the function to be called by some other user 's code and want to restrict the call somehow.

 if ($superuser) {     function get_user_info($user_id, $verbosity_level = 5)     {         ...     } } else {     function get_user_info($user_id)     {         $verbosity_level = 1;         ...     } } 

Comments

In the example, if the user calling get_user_info() is a superuser, she will be able to pass an additional $verbosity_level parameter. A non-superuser caller will get an error if she tries to pass that parameter.



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net