Jump to content

ob_start inside a function


Minklet

Recommended Posts

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?

Link to comment
Share on other sites

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());
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.