Php 使用extract($variables)获取内容,但变量未定义
我还没有掌握extract()函数的诀窍,以及如何传输变量。 我在用户控制器中有一个方法,其中定义了一些变量,并以数组的形式发送给父控制器中的视图函数,在父控制器中提取数组。那么视图是必需的。但是这些变量没有定义。但是可以打印数组内容 以下是具有简化配置文件功能的用户控制器:Php 使用extract($variables)获取内容,但变量未定义,php,arrays,variables,model-view-controller,extract,Php,Arrays,Variables,Model View Controller,Extract,我还没有掌握extract()函数的诀窍,以及如何传输变量。 我在用户控制器中有一个方法,其中定义了一些变量,并以数组的形式发送给父控制器中的视图函数,在父控制器中提取数组。那么视图是必需的。但是这些变量没有定义。但是可以打印数组内容 以下是具有简化配置文件功能的用户控制器: class User extends Controller{ public function profile(){ $profiledetails = $this->profiledet
class User extends Controller{
public function profile(){
$profiledetails = $this->profiledetails();
$profilestatus = $this->profileStatus();
$this->view('profile', [$profiledetails, $profilestatus]);
}}
变量被发送到父控制器中的view函数:
class Controller {
public function view($view, $variables =[]){
extract($variables);
require_once './app/views/' . $view . '.php';
}}
在“profile.php”视图中,显示了未定义的变量错误。我认为“extract()”函数将使$profiledetails和$profilestatus作为变量在视图中可用。
我做错了什么?也许我使用了错误的数组类型,或者我应该使用“variabe variables”之类的。。(在这种情况下,如何?。
提取
与关联数组一起使用
$this->view('profile',
[
'profiledetails' => $profiledetails,
'profilestatus' => $profilestatus
]);
extract
需要一个关联数组。根据文档,它需要:非常棒。谢谢