Are there any issues with using static keyword in a plain php function? -
for example:
<?php function get_current_user_id(){ static $id; if(!$id){ $id = 5; echo "id set."; } return $id; } $id = get_current_user_id(); $id2 = get_current_user_id(); $id3 = get_current_user_id(); echo "ids: ".$id." ".$id2." ".$id3; ?>
//output: id set.ids: 5 5 5
thus presumably repeated calls user id simple return of id still in memory. don't know if idiomatic use of php functions, though. it's kinda singleton, except not oo, , i've never heard of or seen other people using it, i'm wondering if there downsides using statics in manner, or if there gotchas should aware of more complex use cases of statics in functions.
so, problems might encounter type of usage of statics?
i don't think there wrong approach -- actually, use myself quite often, "very short-lifetime caching mecanism", great/useful when have function called lot of times, , heavy calculations (like database query) return same value given execution of script.
and know i'm not 1 doing : drupal, instance, uses mecanism lot -- cache, too.
the "problem" see when want "clear cache" : have pass additionnal parameter function ; , code couple of lines "reset" static cache when parameter set.
Comments
Post a Comment