Reputation: 15949
My host suddenly changed something , and now my sites ( most wp - around 100 ) are getting the infamous error Invalid opcode 153/1/8
The line(s) responsible for it :
$f = function() use ($out) {
echo $out;
};
After 2 minutes of research it appears that the culprit is eAccelerator , which does not support anonymous functions
Both the following questions blamed the error on eAccelerator as well :
Invalide OpCode and php sort function,
https://stackoverflow.com/a/12085901/1244126
Fun fact : the same code was already before a subject of my own 2 questions here on SE and here , where I encountered a problem while using
anonymous functions with older PHP versions ( < 5.3 ) with create_function
$f = create_function(' $out ',' global $out; echo $out;');
So, my question is : how can I change my code in a manner that will avoid the eAccelerator bug AND will work on all php versions . ( it is unlikely that I can convince my host to change something on it´s side )
EDIT I :
For sake of clarity ( although might be slightly irrelevant - the the question is how to have a cross-compatible anonymous functions ) - I am posting the whole relevant code ...
if ( count( $content_widget ) > 0 ) { // avoid error when no widget...
$i=0;
foreach ( $content_widget as $wid ){
$out = null;
$i++;
$widg_id = 'o99_dashboard_widget_dyn_' . $i;
$widg_name = 'widget name - ' . $i;
$out = $wid;
// $f = create_function('$out','global $out;echo $out;');
// $f = create_function('', 'global $out; echo $out ;');
$f = function() use ($out) {
echo $out;
};
// function() use ($out) // NOPE
// $f = $f($out); // NOPE again
wp_add_dashboard_widget($widg_id, $widg_name, $f);
// $i++;
}
}
It is just a simple code to dynamically create dashboard widgets in wp admin area..
Upvotes: 1
Views: 1185
Reputation: 15464
It seems that they are using call_user_func
So, you could create new object and pass callable array.
class s {
private $_out = null;
public function __construct($out){
$this->_out = $out;
}
public function a(){
echo $this->_out;
}
}
$function = array(new S('my out'), 'a');
var_dump(is_callable($function));
call_user_func($function);
Upvotes: 0