首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PHP -将具有私有属性的对象数组保存在会话中,然后收集它们。

PHP -将具有私有属性的对象数组保存在会话中,然后收集它们。
EN

Stack Overflow用户
提问于 2022-06-02 09:30:16
回答 1查看 32关注 0票数 -2

我有一个月类对象数组。这样我就不用不断地检索信息了。

月班如下所示:

代码语言:javascript
运行
复制
<?php

class Month{

    private static $mes;
    private static $year;

    static function void(){
        return new self(null, null);
    }

    function __construct($mes, $year){
        $this->mes = $mes;
        $this->year = $year;
    }

    // GETTERS 
    public function getMes(){
        return $this->mes;
    }

    public function getYear(){
        return $this->year;
    }

    //SETTERS

    public function setMes($mes){
        $this->mes = $mes;
    }

   public function setYear($year){
        $this->year = $year;
    }

    //Métodos
    public function toString(){
        switch ($this->mes) {
            case 1:
                return "Enero - ".$this->year;
                break;
            case 2:
                return "Febrero - ".$this->year;
                break;
            case 3:
                return "Marzo - ".$this->year;
                break;
            case 4:
                return "Abril - ".$this->year;
                break;
            case 5:
                return "Mayo - ".$this->year;
                break;
            case 6:
                return "Junio - ".$this->year;
                break;
            case 7:
                return "Julio - ".$this->year;
                break;
            case 8:
                return "Agosto - ".$this->year;
                break;
            case 9:
                return "Septiembre - ".$this->year;
                break;
            case 10:
                return "Octubre - ".$this->year;
                break;
            case 11:
                return "Noviembre - ".$this->year;
                break;
            case 12:
                return "Diciembre - ".$this->year;
                break;
        }
    }
}

然后,在显示信息的文件中,我有以下代码:

代码语言:javascript
运行
复制
require_once '../Model/ClassEmpleado.php';
require_once '../Model/ClassMonth.php';
include '../Control/CListado-Leads.php';

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if(!isset($_SESSION['meses'])){
    $_SESSION['meses'] = getMonths();
    $meses = $_SESSION['meses'];
    var_dump($_SESSION['meses']);
}
else{
    $meses = $_SESSION['meses'];
    var_dump($_SESSION['meses']);
}

最后,下面显示了一个select,并为数组中的每个对象提供了一个选项。

代码语言:javascript
运行
复制
    <div class="mt-5 container-fluid">
        <form action='#' method='POST'>
            Mes: <select id='filtromes' name='filtromes' class="filtro-mes">
                <option class='opciones-filtro-mes' value='0'>Histórico Completo</option>
                
                <?php 
                    for($i = 0; $i < count($meses); $i++){
                        $mes = $meses[$i];
                        echo "<option class='opciones-filtro-mes' value='" . $mes->getMes() . "-" . $mes->getYear() . "'>".$meses[$i]->toString() . "</option>";
                    }
                ?>
            </select>
        </form>
    </div>

所有这些都很好,当我第一次访问文件时,我收集信息并将其存储在会话中。

问题出现在以后,当重新加载页面,或者从不同的页面再次访问它时,会话中的数组似乎已经改变,不再工作。

如果我var_dump会话变量,我将得到以下输出:

第一次:

代码语言:javascript
运行
复制
array(15) { [0]=> object(Month)#4 (2) { ["mes"]=> string(1) "4" ["year"]=> string(4) "2021" } [1]=> object(Month)#5 (2) { ["mes"]=> string(1) "5" ["year"]=> string(4) "2021" } [2]=> object(Month)#6 (2) { ["mes"]=> string(1) "6" ["year"]=> string(4) "2021" } [3]=> object(Month)#7 (2) { ["mes"]=> string(1) "7" ["year"]=> string(4) "2021" } [4]=> object(Month)#8 (2) { ["mes"]=> string(1) "8" ["year"]=> string(4) "2021" } [5]=> object(Month)#9 (2) { ["mes"]=> string(1) "9" ["year"]=> string(4) "2021" } [6]=> object(Month)#10 (2) { ["mes"]=> string(2) "10" ["year"]=> string(4) "2021" } [7]=> object(Month)#11 (2) { ["mes"]=> string(2) "11" ["year"]=> string(4) "2021" } [8]=> object(Month)#12 (2) { ["mes"]=> string(2) "12" ["year"]=> string(4) "2021" } [9]=> object(Month)#13 (2) { ["mes"]=> string(1) "1" ["year"]=> string(4) "2022" } [10]=> object(Month)#14 (2) { ["mes"]=> string(1) "2" ["year"]=> string(4) "2022" } [11]=> object(Month)#15 (2) { ["mes"]=> string(1) "3" ["year"]=> string(4) "2022" } [12]=> object(Month)#16 (2) { ["mes"]=> string(1) "4" ["year"]=> string(4) "2022" } [13]=> object(Month)#17 (2) { ["mes"]=> string(1) "5" ["year"]=> string(4) "2022" } [14]=> object(Month)#18 (2) { ["mes"]=> string(1) "6" ["year"]=> string(4) "2022" } }

从另一个页面重新加载或访问时:

代码语言:javascript
运行
复制
array(15) { [0]=> object(Month)#2 (2) { ["mes":"Month":private]=> string(1) "4" ["year":"Month":private]=> string(4) "2021" } [1]=> object(Month)#3 (2) { ["mes":"Month":private]=> string(1) "5" ["year":"Month":private]=> string(4) "2021" } [2]=> object(Month)#4 (2) { ["mes":"Month":private]=> string(1) "6" ["year":"Month":private]=> string(4) "2021" } [3]=> object(Month)#5 (2) { ["mes":"Month":private]=> string(1) "7" ["year":"Month":private]=> string(4) "2021" } [4]=> object(Month)#6 (2) { ["mes":"Month":private]=> string(1) "8" ["year":"Month":private]=> string(4) "2021" } [5]=> object(Month)#7 (2) { ["mes":"Month":private]=> string(1) "9" ["year":"Month":private]=> string(4) "2021" } [6]=> object(Month)#8 (2) { ["mes":"Month":private]=> string(2) "10" ["year":"Month":private]=> string(4) "2021" } [7]=> object(Month)#9 (2) { ["mes":"Month":private]=> string(2) "11" ["year":"Month":private]=> string(4) "2021" } [8]=> object(Month)#10 (2) { ["mes":"Month":private]=> string(2) "12" ["year":"Month":private]=> string(4) "2021" } [9]=> object(Month)#11 (2) { ["mes":"Month":private]=> string(1) "1" ["year":"Month":private]=> string(4) "2022" } [10]=> object(Month)#12 (2) { ["mes":"Month":private]=> string(1) "2" ["year":"Month":private]=> string(4) "2022" } [11]=> object(Month)#13 (2) { ["mes":"Month":private]=> string(1) "3" ["year":"Month":private]=> string(4) "2022" } [12]=> object(Month)#14 (2) { ["mes":"Month":private]=> string(1) "4" ["year":"Month":private]=> string(4) "2022" } [13]=> object(Month)#15 (2) { ["mes":"Month":private]=> string(1) "5" ["year":"Month":private]=> string(4) "2022" } [14]=> object(Month)#16 (2) { ["mes":"Month":private]=> string(1) "6" ["year":"Month":private]=> string(4) "2022" } }

如您所见,会话中的变量已经更改。看起来变量已经被序列化了,或者类似的东西。

如果我把类的属性作为公共属性,它就能很好地工作。但为了安全起见,我希望他们能保密。

EN

回答 1

Stack Overflow用户

发布于 2022-06-02 09:57:47

首先,

代码语言:javascript
运行
复制
$messe = $_SESSION['meses']; 
// will create copy of $_SESSION['meses']
// changes to $messe wont be saved to $_SESSION['meses']

您应该使用别名/引用,即:

代码语言:javascript
运行
复制
$messe =& $_SESSION['meses'];
if (!isset($messe)) {
    $messe = getMonths(); 
}
var_dump($_SESSION['meses']);
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72473923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档