我还没有得到提取()函数的诀窍,也没有传递变量。我在用户控制器中有一个方法,其中定义了一些变量,并将数组中的一个数组发送到父控制器中的视图函数,在该函数中提取数组。那么视图是必需的。但是变量却没有定义。但是可以打印数组内容。
下面是具有简化配置文件功能的用户控制器:
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‘中,将显示未定义的变量错误。我认为“提取()”函数将使$profiledetails和$profilestatus作为视图中的变量可用。我做错了什么?也许我使用了错误的数组类型,或者我应该使用“变量变量”之类的。(在这种情况下,怎么做?)
发布于 2015-05-30 23:40:14
extract使用关联数组。
$this->view('profile',
[
'profiledetails' => $profiledetails,
'profilestatus' => $profilestatus
]); https://stackoverflow.com/questions/30552397
复制相似问题