I have a $c
variable that is calculated at the beginning of the request. After calculating this several nested methods use it as a part of their job. Is it better that I pass down the $c
variable to every method or store it based on the request (cache it maybe?) and use the stored value in each method to get the value of $c
.
- Approach one:
$c = 'time consuming calculated value`
a($c);
b($c);
e($c);
function a($c){
// some work using $c
}
function b($c){
// some work using $c
}
function c($c){
// some work using $c
}
function d($c){
// some work using $c
}
function e($c) {
// some work
d($c)
// some other work
}
- Approach two
$c = 'time consuming calculated value`;
cache::set('unique-key', $c);
function a(){
// some work using cache::get('unique-key');
}
function b(){
// some work using cache::get('unique-key');
}
function c(){
// some work using cache::get('unique-key');
}
function d(){
// some work using cache::get('unique-key');
}
function e() {
// some work
d();
// some other work
}
I'm using the first approach right know to decouple the methods more (from data gathering) but unsure of what approach is better or are there any other approaches?