Jump to content

Variables and Overridden functions?


random1

Recommended Posts

I've written the following code:

 

$mode = 'development'; // 'Development' or 'Public'

// Writes a buffer to a file
/**
* ob_file_callback()
* Writes the generated CSS to a file.
* @param mixed $buffer
* @return void
*/
function ob_file_callback($buffer)
{
// stylesheet-public.css is the output generated file
$ob_file = fopen('stylesheet-' . $mode . '.css','w');

fwrite($ob_file, $buffer);
}

$buffer = 'test';
ob_start('ob_file_callback');

 

$mode is always empty when I run this code.

 

Is it an issue because ob_file_callback is an overridden function?

Link to comment
https://forums.phpfreaks.com/topic/230185-variables-and-overridden-functions/
Share on other sites

Variables created outside of functions do not exist within functions. That's the point of functions, to create stand alone code.

 

If you want to use $mode within your function you will need to pass it in as an argument.

 

(And please, no one mention the globals keyword, globals break encapsulation)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.