PHP Developers Cookbook (2nd Edition)

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.

Категории