Overloading is a very useful feature of some programming languages to objects. However tuti OO languages do not support it, and some of the "implementing" or limited to or different. In general, functions or methods, overloading allows you to create two or more functions / methods that have the same name but accept different parameters, for example:
1 2 3 4 5 6 7 8 9 10 11 | function sum (a: int, b: int) { / / Code } function sum (a: float, b: float) { / / Code } function sum (a: string, b: string, c: string) { / / Code } |
Choosing the right function to use is, of course, dictated solely by the "type" (and also the number) of parameters passed to the function itself. You can, cone a few extra lines of code, emulate this behavior, even in languages that do not support it directly. For example in PHP you can create a function like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | / ** * This function behaves differently based on the input parameters. * In this example we can "understand" how to behave based on the analysis * Of the input parameters and then: * * - Recover even if the parameters passed in a different way * - The function to execute different code based on input parameters * * * / $id_qualcosa = '' ) { get_qualcosa function ($ id_qualcosa ='') { func_num_args ( ) > 0 ) { // numero degli argomenti if ( func_num_args ()> 0) {/ / number of arguments func_get_arg ( 0 ) ; // prendo il primo $ Arg = func_get_arg (0) / / take the first is_array ( $arg ) ) { // se il primo argomento è un array... if ( is_array ($ arg)) {/ / if the first argument is an array ... $arg [ 'id_qualcosa' ] ; // faccio qualcosa o recupero il parametro Id_qualcosa $ = $ args ['id_qualcosa'], / / do something or recover the parameter } } } |
This technique can be useful when we have a function that can be called in different fields. For example, imagine you have a function that extracts information from a database of a user. This could take the form:
1 2 3 4 5 6 7 8 9 10 11 12 13 | / ** * Get the user information via the id * * @ Param $ id ID of the database * / $id_user = '' ) { get_user_info function ($ id_user ='') { func_num_args ( ) > 0 ) { // numero degli argomenti if ( func_num_args ()> 0) {/ / number of arguments func_get_arg ( 0 ) ; // prendo il primo $ Arg = func_get_arg (0) / / take the first is_array ( $arg ) ) { // se il primo argomento è un array... if ( is_array ($ arg)) {/ / if the first argument is an array ... $arg [ 'id_user' ] ; // $arg === $_POST Id_user $ = $ args ['id_user'] / / $ arg === $ _POST } } } |
This feature can be used within a library ( mialib.php ) and called from other PHP functions. If we use Ajax techniques or simple form, it would be nice to reuse the same function without building one specifically. If so we have a format that requires using a menu select the user's choice:
1 2 3 4 5 6 7 8 9 | "post" action = "sample.php" > < form method = "post" action = "sample.php"> "id_user" > < select name = "id_user"> "1" > Utente 1 < / option > < option value = "1"> User 1 </ option > "2" > Utente 2 < / option > < option value = "2"> User 2 </ option > "3" > Utente 3 < / option > < option value = "3"> User 3 </ option > </ select > "hidden" name = "comando" value = "get_user_info" / > < input type = "hidden" name = "command" value = "get_user_info" /> "submit" value = "Invia" / > < input type = "submit" value = "Submit" /> </ form > |
Our sample.php could be:
1 2 3 4 | / / Include the file containing my functions ; include_once "mialib.php"; / / Call the function passing an array my $ _POST 'echo ' . $_POST [ 'comando' ] . "( \$ _POST );" ) ; eval ('echo'. $ _POST ['command']. "(\ $ _POST);"); |










There are no comments for this post
Leave a comment