John Davidson

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

0 comments
Message:


I have an array of Month class objects. Which I keep in session so I don't have to constantly retrieve the information.


The Month class looks like this:


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


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.



My Answer:
php

class Month {
private $name;
private $days;

public function __construct($name, $days) {
$this->name = $name;
$this->days = $days;
}

public function getName() {
return $this->name;
}

public function getDays() {
return $this->days;
}
}

// Create an array of Month objects
$months = [
new Month('January', 31),
new Month('February', 28),
new Month('March', 31),
new Month('April', 30),
new Month('May', 31),
new Month('June', 30),
new Month('July', 31),
new Month('August', 31),
new Month('September', 30),
new Month('October', 31),
new Month('November', 30),
new Month('December', 31)
];

// Save the array of Month objects in session
session_start();
$_SESSION['months'] = $months;

// Retrieve the array of Month objects from session
$monthsFromSession = $_SESSION['months'];

// Output the names and days of each Month object
foreach ($monthsFromSession as $month) {
echo $month->getName() . ' has ' . $month->getDays() . ' days.
';
}

?>

Rate this post

3 of 5 based on 6075 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us