我还没有得到extract()函数,并且传输变量。 我在用户控制器中有一个方法,其中定义了一些变量,并在数组中发送到父控制器中的视图函数,在该控制器中提取数组。然后视图是必需的。但变量未定义。可以打印数组内容。
以下是具有简化配置文件功能的用户控制器:
class User extends Controller{
public function profile(){
$profiledetails = $this->profiledetails();
$profilestatus = $this->profileStatus();
$this->view('profile', [$profiledetails, $profilestatus]);
}}
变量被发送到父控制器中的视图函数:
class Controller {
public function view($view, $variables =[]){
extract($variables);
require_once './app/views/' . $view . '.php';
}}
在视图中,' profile.php',显示未定义的变量错误。我以为" extract()"函数将使$ profiledetails和$ profilestatus可用作视图中的变量。 我究竟做错了什么?也许我使用了错误类型的数组,或者我应该使用"变量变量"或某事......(在这种情况下,如何?)。
答案 0 :(得分:7)
extract
适用于关联数组。
$this->view('profile',
[
'profiledetails' => $profiledetails,
'profilestatus' => $profilestatus
]);