简体   繁体   English

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

[英]PHP - Save in session an array of objects with private attributes, and then collect them

I have an array of Month class objects.我有一个 Month 类对象的数组。 Which I keep in session so I don't have to constantly retrieve the information.我保持在会话中,因此我不必不断地检索信息。

The Month class looks like this: Month 类如下所示:

<?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;
        }
    }
}

Then in the file that shows the information, I have the following code:然后在显示信息的文件中,我有以下代码:

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']);
}

And finally, below, I show a select with an option for each object in the array.最后,在下面,我为数组中的每个对象显示了一个带有选项的选择。

    <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>

All this works fine, the first time I access the file, I collect the information and store it in session.所有这些工作正常,第一次访问文件时,我收集信息并将其存储在会话中。

在此处输入图像描述

The problem comes later, when reloading the page, or accessing it again from a different page, the array in session seems to have changed and no longer works.问题稍后出现,当重新加载页面或从不同页面再次访问时,会话中的数组似乎已更改并且不再有效。

在此处输入图像描述

If I var_dump the session variables, I get this output:如果我 var_dump 会话变量,我会得到以下输出:

The first time:第一次:

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" } }

When reloading or accessing from another page:从其他页面重新加载或访问时:

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" } }

As you can see the variable in session has changed.如您所见,会话中的变量已更改。 It looks like the variable has been serialized, or something like that.看起来变量已被序列化,或者类似的东西。

If I put the attributes of the class as public, it works perfectly.如果我将类的属性设为公开,它会完美运行。 But for security I would like them to be private.但为了安全起见,我希望它们是私密的。

First of all,首先,

$messe = $_SESSION['meses']; 
// will create copy of $_SESSION['meses']
// changes to $messe wont be saved to $_SESSION['meses']

you should use alias/references , ie:你应该使用alias/references ,即:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM