我在wordpress主题中看到了这个函数。
function get( $key ) {
if ( method_exists( $this, 'get_' . $key ) ) {
return call_user_func_array( array( $this, 'get_' . $key ), array() );
} elseif ( property_exists( $this, $key ) ) {
return $this->{$key};
}
return false;
}为什么在$ $key ->{$key}中需要{}?为什么$这个->$key不行?谢谢。
发布于 2020-08-31 14:29:20
当我们在任何变量中使用$时,它就变成了变量(变量vars),比如$$name。
检查此引用中是否存在变量。
https://www.php.net/manual/en/language.variables.variable.php
在这里,$this->$key将成为变量var,因为它涉及类的公共/私有/受保护变量。
因此,{}在$this->{$key}中将设置类的$key vars变量。
https://wordpress.stackexchange.com/questions/374025
复制相似问题