使用extract($ variables)获取内容,但变量未定义

Gal*_*van 5 php arrays variables model-view-controller extract

我没有得到extract()函数的挂起,并且传输变量.我在用户控制器中有一个方法,其中定义了一些变量,并在数组中发送到父控制器中的视图函数,在该控制器中提取数组.然后视图是必需的.但变量未定义.可以打印数组内容.

以下是具有简化配置文件功能的用户控制器:

class User extends Controller{

    public function profile(){

         $profiledetails = $this->profiledetails();           
         $profilestatus = $this->profileStatus();            

         $this->view('profile', [$profiledetails, $profilestatus]);
}}    
Run Code Online (Sandbox Code Playgroud)

变量被发送到父Controller中的view函数:

class Controller {
    public function view($view, $variables =[]){                    

    extract($variables);

    require_once './app/views/' . $view . '.php';
}}
Run Code Online (Sandbox Code Playgroud)

在视图中,'profile.php',显示未定义的变量错误.我认为"extract()"函数会使$ profiledetails和$ profilestatus在视图中可用作变量.我究竟做错了什么?也许我正在使用错误类型的数组,或者我应该使用"变量变量"或其他东西..(在这种情况下,如何?).

Ali*_*ani 7

extract使用关联数组.

    $this->view('profile', 
      [
        'profiledetails' => $profiledetails, 
        'profilestatus' => $profilestatus
      ]);   
Run Code Online (Sandbox Code Playgroud)

  • 我的意思是我确定.让我编辑一下. (2认同)