我有一个月类对象数组。这样我就不用不断地检索信息了。
月班如下所示:
<?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;
}
}
}
然后,在显示信息的文件中,我有以下代码:
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,并为数组中的每个对象提供了一个选项。
<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会话变量,我将得到以下输出:
第一次:
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" } }
从另一个页面重新加载或访问时:
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" } }
如您所见,会话中的变量已经更改。看起来变量已经被序列化了,或者类似的东西。
如果我把类的属性作为公共属性,它就能很好地工作。但为了安全起见,我希望他们能保密。
发布于 2022-06-02 09:57:47
首先,
$messe = $_SESSION['meses'];
// will create copy of $_SESSION['meses']
// changes to $messe wont be saved to $_SESSION['meses']
您应该使用别名/引用,即:
$messe =& $_SESSION['meses'];
if (!isset($messe)) {
$messe = getMonths();
}
var_dump($_SESSION['meses']);
https://stackoverflow.com/questions/72473923
复制相似问题