Reputation: 2291
When switching from PHP 5.6 to 7.0, I need to do it based on this script. I am trying to apply rewrite rules in WordPress as follows but a strange PHP issue is encountered. Dynamic properties made via variable are not working in PHP 7.0.
$rule = [ 'name' => 'profile',
'rule' => 'author_base',
'rewrite' => 'profile',
'position' => 'top',
'replace' => true,
'dynamic' => true
];
global $wp_rewrite;
global $wp;
$wp->add_query_var($rule['name']);
if(isset($rule['replace']) && $rule['replace']) {
var_dump($rule['rule']); // author_base
$wp_rewrite->$rule['rule']=$rule['rewrite']; // this doesn't work
$wp_rewrite->author_base=$rule['profile']; // this works
var_dump($wp_rewrite->$rule['rule']) // return null => BAD
var_dump($wp_rewrite->author_base); // returns 'author' => OK
// In PHP 5.6 Works both including $wp_rewrite->$rule['rule']
} else {
add_rewrite_rule($rule['rule'], $rule['rewrite'], $rule['position']);
}
Upvotes: 1
Views: 52
Reputation: 36944
That's one of the B.C of php7.
Indirect access to variables, properties, and methods will now be evaluated strictly in left-to-right order, as opposed to the previous mix of special cases.
| Expression | PHP 5 interpretation | PHP 7 interpretation |
|---------------------|-----------------------|-----------------------|
| $foo->$bar['baz']() | $foo->{$bar['baz']}() | ($foo->$bar)['baz']() |
So, change
$wp_rewrite->$rule['rule']=$rule['rewrite'];
var_dump($wp_rewrite->$rule['rule']);
to
$wp_rewrite->{$rule['rule']}=$rule['rewrite'];
var_dump($wp_rewrite->{$rule['rule']});
Upvotes: 1