I need to extract about 30 variables from MySQL for use in calculations. The calculations aren’t all that complicated, but I can’t tell in advance which variables I’ll need, so it seems best to extract them all.
I’m using a PHP function, so I started by declaring all the variables as ‘global’:-
function calcBandbCost($setYr, $rate) {
global $bb_cost, $yr_id, $w_rate_1, $w_rate_2, $s_rate, $o_rate, $x_rate, $t_rate, $c2_rate, $c1_rate, $b_rate, $t_age, $c2_age, $c1_age, $a_age, $cot_use, $cot_min, $min_dep, $max_pers, $next_bk_ref, $next_guest_id, $adv_days, $one_night_sup, $single_sup, $four_disc, $pp_dep, $pp_full, $first_date, $last_date, $link;
...
// stuff to do MySQL query, extracting values for all these variables
...
// stuff to do the calculations
...
}
I almost certainly won’t need all those to be accessible outside the function, so I should be able to pare the list down in due course.
Then I realised I’d need to call most of these variables at other points in the application, so I split out the MySQL call into a function of its own. But now I have to declare all those variables as ‘global’ in the MySQL call function in order to have them available for the calculation function (and for other functions too).
So now (for the moment, anyway) I’m declaring 30 or more variables as ‘global’ in two functions only a few lines apart in the file.
// Get rates from DB:
function getRates($link, $setYr, $rate) {
global $bb_cost, $yr_id, $w_rate_1, $w_rate_2, $s_rate, $o_rate, $x_rate, $t_rate, $c2_rate, $c1_rate, $b_rate, $t_age, $c2_age, $c1_age, $a_age, $cot_use, $cot_min, $min_dep, $max_pers, $next_bk_ref, $next_guest_id, $adv_days, $one_night_sup, $single_sup, $four_disc, $pp_dep, $pp_full, $first_date, $last_date, $link;
global $nights, $adults, $teens, $child2, $child1, $baby, $cot_req, $dep_amt;
$query = "SELECT `yr_id`, `w_rate_1`, `w_rate_2`, `s_rate`, `o_rate`, `x_rate`, `t_rate`, `c2_rate`, `c1_rate`, `b_rate`, `t_age`, `c2_age`, `c1_age`, `a_age`, `cot_use`, `cot_min`, `min_dep`, `max_pers`,`adv_days`, `one_night_sup`, `single_sup`, `four_disc`, `pp_dep`, `pp_full` from rates WHERE yr_id = " . $setYr;
...
// stuff to do MySQL query
...
}
//---------------------------
// Calculate B&B cost:
function calcBandbCost($setYr, $rate) {
global $bb_cost, $yr_id, $w_rate_1, $w_rate_2, $s_rate, $o_rate, $x_rate, $t_rate, $c2_rate, $c1_rate, $b_rate, $t_age, $c2_age, $c1_age, $a_age, $cot_use, $cot_min, $min_dep, $max_pers, $next_bk_ref, $next_guest_id, $adv_days, $one_night_sup, $single_sup, $four_disc, $pp_dep, $pp_full, $first_date, $last_date, $link;
global $nights, $adults, $teens, $child2, $child1, $baby, $cot_req, $dep_amt;
...
// stuff to do the calculations
...
}
It works, but I feel there must be a better way, avoiding the repetition, which is only going to get worse when I want to use these variables in another function. I’m using procedural code. I suspect it may be easier with OOP but I’m not ready to go there yet.
I’ve considered saving them all to $_SESSION and extracting when required, but is there perhaps a better, recommended, way ?
Perhaps it will become blindingly obvious the moment I post this thread (not an unknown phenomenon).