猿问

访问另一个功能会话

我想多次访问一个数组,为此,我将数组与以下会话声明一起存储在Session数组中


$_SESSION['data'] = array();

有没有可能像公共变量一样使用$ _SESSION


最初,它调用函数index(),然后单击另一个按钮,将formval设置为1并转到函数index2


  


<?php


new SampleController();

session_start();

class SampleController {

    public function __construct() {

        if (isset($_REQUEST['formval']) && $_REQUEST['formval'] == '1') {

            self::index2();

        } else {

            self::index();

        }

    }


    public function getfunc1() {

        $_SESSION['data'] = array();

        $datarray = array('0', '1', '2');

        $_SESSION['data'] = $datarray;

    }


    public function getfunc2() {

        print_r($_SESSION);

        $val = $_SESSION['data'];

    }

}

它显示像


未定义的变量:_SESSION


我希望需要访问$_SESSION['data']阵列


慕雪6442864
浏览 157回答 3
3回答

慕田峪9158850

尝试此方法将为您工作。<?php&nbsp;session_start();class SampleController {&nbsp; &nbsp; public function getfunc1(){&nbsp; &nbsp; &nbsp; &nbsp; $datarray = array('0','1','2');&nbsp; &nbsp; &nbsp; &nbsp; $_SESSION['data'] = $datarray;&nbsp; &nbsp; }&nbsp; &nbsp; public function getfunc2(){&nbsp; &nbsp; &nbsp; &nbsp; return $_SESSION['data'];&nbsp; &nbsp; }}$SampleController = new SampleController();$SampleController->getfunc1();$data = $SampleController->getfunc2();print_r($data);

倚天杖

这是错的new SampleController();session_start();首先开始会议session_start();new SampleController();
随时随地看视频慕课网APP
我要回答