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
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)

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.