Function inside function in php: error: cannot override

I have this php script:

function hoeveelzijner ($jaar, $id) { function hoeveelhoeveel($beginstamp, $endstamp, $id) { $dates = mysql_query('SELECT v_date FROM visitors WHERE id="'.$id.'" AND v_date<"'.$endstamp.'" AND v_date>"'.$beginstamp.'"'); return mysql_num_rows($dates); } $i = 1; while ($i < 13) { $hoeveel[$i-1] = hoeveelhoeveel(mktime(0, 0, 0, $i, 1, $jaar),mktime(0, 0, 0, $i, cal_days_in_month(CAL_GREGORIAN,$i,$jaar),$jaar),$id); $i = $i+1; } return $hoeveel; } 

When I put this under it, it works fine:

 $values = hoeveelzijner(2005, 1); 

However, when I do this twice, for example:

 $values = hoeveelzijner(2005, 1); $test = hoeveelzijner(2000, 4); 

I get this error: Fatal error: Unable to override hoeveelhoeveel () (previously declared in ...: 69) to ... on line 69.

Does anyone know what I'm doing wrong? It seems to destroy the purpose of using functions, if I can use it only once ...

Additional information: I do not include any other files and do not update the function anywhere else in the script.

Thank you so much!

+7
source share
3 answers

You can not only use it once; You can declare it only once. Each time the hoeveelzijner function is used, the hoeveelzijner function is hoeveelhoeveel . If you call it more than once, you will update it, which is prohibited.

You have two options: the first is the definition of a function outside the contained function. The function will be declared when the file is first parsed and then reused. If you want to limit the definition of a function to a specific area (this is a good idea in principle), you can use the anonymous function syntax introduced in PHP 5.3:

 function hoeveelzigner($jaar, $id) { $hoeveelhoeveel = function($beginstamp, $endstamp, $id) { $dates = mysql_query('SELECT v_date FROM visitors WHERE id="'.$id.'" AND v_date<"'.$endstamp.'" AND v_date>"'.$beginstamp.'"'); return mysql_num_rows($dates); }; // etc } 

Then you can use the function as $hoeveelhoeveel(...) .

+12
source

Although PHP allows you to declare a function anywhere, I think that what you are doing is not considered best practice.

For most people, this just looks wrong.

You should simply declare the function outside the parent function.

Alternatively, you can add function_exists() around the inner function, although again I would just declare the inner function outside the parent function.

Perhaps even make a class for him.

0
source

You cannot declare it again, especially if the function calls it more than once. Here is a simple change that should work.

 function hoeveelhoeveel($beginstamp, $endstamp, $id) $dates = mysql_query('SELECT v_date FROM visitors WHERE id="'.$id.'" AND v_date<"'.$endstamp.'" AND v_date>"'.$beginstamp.'"'); return mysql_num_rows($dates); } function hoeveelzijner ($jaar, $id) { $i = 1; while ($i < 13) { $hoeveel[$i-1] = hoeveelhoeveel(mktime(0, 0, 0, $i, 1, $jaar),mktime(0, 0, 0, $i, cal_days_in_month(CAL_GREGORIAN,$i,$jaar),$jaar),$id); $i = $i+1; } return $hoeveel; } 

Then you can call any function in the same way in other functions, if necessary.

0
source

All Articles