ciber Posted December 18, 2008 Share Posted December 18, 2008 Is it possible to store information for the duration of the function, so that the script doesn't need to download an an API's XML everytime it is requesting something? Link to comment https://forums.phpfreaks.com/topic/137563-solved-store-data-for-function/ Share on other sites More sharing options...
rhodesa Posted December 18, 2008 Share Posted December 18, 2008 can you elaborate on the duration? do you mean for the duration of a script executing? a user's session? a specific period of time? Link to comment https://forums.phpfreaks.com/topic/137563-solved-store-data-for-function/#findComment-718976 Share on other sites More sharing options...
ciber Posted December 18, 2008 Author Share Posted December 18, 2008 while the script is executing Link to comment https://forums.phpfreaks.com/topic/137563-solved-store-data-for-function/#findComment-718978 Share on other sites More sharing options...
rhodesa Posted December 18, 2008 Share Posted December 18, 2008 why can't you just store it in a variable? can you provide an example of how you are using it now, which calls the api's xml more then once? Link to comment https://forums.phpfreaks.com/topic/137563-solved-store-data-for-function/#findComment-718980 Share on other sites More sharing options...
ciber Posted December 18, 2008 Author Share Posted December 18, 2008 i have a function which request data from the YouTube API - here is my code, perhaps this can explain why function stats($id,$type) { $data = getdata($id); if ($type == '1') { preg_match_all( "<yt:statistics viewCount='(.*?)' favoriteCount='(.*?)'/>", $data, $stat); $final = $stat[1][0]; } if ($type == '2') { preg_match_all( "<yt:statistics viewCount='(.*?)' favoriteCount='(.*?)'/>", $data, $stat); $final = $stat[2][0]; } return $final; } you see, my function does 2 things, so if I need to request it again, it will then download the data again or will it hold it .. sorry im still new to this... Link to comment https://forums.phpfreaks.com/topic/137563-solved-store-data-for-function/#findComment-718982 Share on other sites More sharing options...
rhodesa Posted December 18, 2008 Share Posted December 18, 2008 have the functions stat return all the stats for that $id as an array, so your script only calls stats() once per $id. function stats($id) { $data = getdata($id); $stats = array(); //Type 1 if(preg_match_all( "<yt:statistics viewCount='(.*?)' favoriteCount='(.*?)'/>", $data, $stat)){ $stats[1] = $stat[1][0]; } //Type 2 if(preg_match_all( "<yt:statistics viewCount='(.*?)' favoriteCount='(.*?)'/>", $data, $stat)){ $stats[2] = $stat[2][0]; } return $stats; } $stats = stats(123); print $stats[1]; print $stats[2]; ...or you could use OOP to make this more structured...do you know OOP? Link to comment https://forums.phpfreaks.com/topic/137563-solved-store-data-for-function/#findComment-718999 Share on other sites More sharing options...
ciber Posted December 18, 2008 Author Share Posted December 18, 2008 no ... i only started to learn this about 3 months ago, and only in the last week have I really started to go into it a lot. I am using the "dummy" way. :| still using the manual on php.net quite a bit. The way you have changed this now, is much simpler! Thanks!! Link to comment https://forums.phpfreaks.com/topic/137563-solved-store-data-for-function/#findComment-719007 Share on other sites More sharing options...
rhodesa Posted December 18, 2008 Share Posted December 18, 2008 np...the OOP way, you would create an instance using the $id. when the instance is created, you would do the API call, and store the $data into a variable inside that instance. then, you could call functions inside that instance which would do your preg_matchs on the $data. OOP sounds scary (and it kind of is), but once you learn it, it's amazing! Link to comment https://forums.phpfreaks.com/topic/137563-solved-store-data-for-function/#findComment-719024 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.