覆盖属性与dinamic属性变量不工作PHP 7.0

时间:2016-12-16 09:00:53

标签: php php-7

从PHP 5.6切换到7.0时。我需要根据这个脚本来做。我试图在wordpress中应用重写规则如下,但遇到一个奇怪的PHP问题。通过变量创建的Dinamic属性在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']);
}       

1 个答案:

答案 0 :(得分:1)

这是php7的B.C之一。

  

现在将严格按照从左到右的顺序对变量,属性和方法进行间接访问,而不是之前的特殊情况组合。

DispatchQueue.main.async {
    let result = Thread.callStackSymbols
}

所以,改变

| Expression          | PHP 5 interpretation  | PHP 7 interpretation  |
|---------------------|-----------------------|-----------------------|
| $foo->$bar['baz']() | $foo->{$bar['baz']}() | ($foo->$bar)['baz']() |

$wp_rewrite->$rule['rule']=$rule['rewrite'];
var_dump($wp_rewrite->$rule['rule']);