Minklet Posted July 15, 2011 Share Posted July 15, 2011 I'm trying to tidy up my code by getting to grips with OOP. I know that this isnt really OOP, but I want to put a cache system into a function. The bit in comments is basically the bit that I want to put into a function. As you can see its fairly rudimentary but basically I dont know how to get what 'include' produces into the output buffer because, of course, its not actually outputting anything. I thought something like fopen() but its all PHP scripts in the include so taht wont work Anyway, this is what I have: $cachefile = 'cache/cache_blogphotofeed.html'; $include = 'feeds/blogphoto_feed.php'; $table = 'our_photos'; if (file_exists($cachefile)) { if (is_outdated($cachefile, $table, $dbc)) { // This bit ob_start(); include($include); $fp = fopen($cachefile, 'w'); fwrite($fp, ob_get_contents()); fclose($fp); // close the file ob_end_flush();[/b] // To this bit } else { include($cachefile); } } else { ob_start(); include($include); $fp = fopen($cachefile, 'w'); fwrite($fp, ob_get_contents()); fclose($fp); // close the file ob_end_flush(); } The function is_outdated simply checks if the database table has been modified more recently than the cache file Any suggestions at all? Quote Link to comment https://forums.phpfreaks.com/topic/242083-ob_start-inside-a-function/ Share on other sites More sharing options...
TeNDoLLA Posted July 15, 2011 Share Posted July 15, 2011 Dunno what this has to do with OOP since there is not one bit of object oriented programming in that code. But something like this would be shorter, if you insist to put it inside function. Code is untested but you get the idea. And you can replace the three functions fopen, fwrite and fclose with file_put_contents(). function doCache($include, $cachefile) { ob_start(); include_once($include); file_put_contents($cachefile, ob_get_clean()); } Quote Link to comment https://forums.phpfreaks.com/topic/242083-ob_start-inside-a-function/#findComment-1243213 Share on other sites More sharing options...
AyKay47 Posted July 15, 2011 Share Posted July 15, 2011 not sure what fopen would do for you here, if you include file doesn't output anything ob is not what you are looking for here, what exactly are you trying to do with the include file? Quote Link to comment https://forums.phpfreaks.com/topic/242083-ob_start-inside-a-function/#findComment-1243215 Share on other sites More sharing options...
TeNDoLLA Posted July 15, 2011 Share Posted July 15, 2011 I guess the include file is the content of the web page. And we wants to cache it if it is not cached and show the content right after. If the file is cached and cache is not expired he will fetch the cached file from the disk, instead of generating it with php. Caching can make page loads even 100x or more faster sometimes. Quote Link to comment https://forums.phpfreaks.com/topic/242083-ob_start-inside-a-function/#findComment-1243217 Share on other sites More sharing options...
requinix Posted July 15, 2011 Share Posted July 15, 2011 if (cache miss) { build cache; } show content from cache Quote Link to comment https://forums.phpfreaks.com/topic/242083-ob_start-inside-a-function/#findComment-1243221 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.