Reputation: 57
I'm writing a Wordpress plugin that needs to tailor part of its output to be compatible with a page caching plugin (W3 Total Cache). The technique used by the caching plugin to cache most of the page and only execute fragments is to eval()
such code fragments.
However, as my plugin may be invoked in several places within a Wordpress template, I need to keep state between these different instances of my code being eval()
-executed. Basically I need the first piece of code to save data that the other code fragments will then use.
Essentially I need the code in the eval()
to access data outside of the eval()
.
Are there any methods of doing this?
EDIT: I should probably add that each execution of eval()
is also wrapped in a function that is called as a callback from preg_replace_callback()
, so any variables set in the eval()
would need to persist through that also
Upvotes: 1
Views: 405
Reputation: 146640
eval() does not create a new variable scope:
<?php
$foo = 'Hello, World!';
eval('var_dump($foo); $bar = 33;');
var_dump($bar);
... prints this:
string(13) "Hello, World!"
int(33)
I'd say all your problem is being able to create global variables from within a function. You can use the usual approaches: global keyword or $GLOBALS array.
Upvotes: 1