I am currently working on a large project for a client, and to help simplify things and make managing sessions easier I have made a session class, this means that if I ever change the way I manage my sessions I can access the structure of the sessions in one file rather then multiple files where a session has been echoed (Such as the users name or the current page)
My class has 3 types of functions setters, getters and unsetters which is kind of self explanatory.
Now in this instance I am setting an error message to the session class directly and then getting the error messages from another class which calls a function from the session within it.
Below are simplified versions on my files showing code I feel is relevent
c_session.php
class session {
private $instance = 'isntname';
private $A_user = 'user';
private $A_page = 'page';
private $A_message = 'message';
//Set messages to the session.
public function set_message($type, $value) {
$_SESSION[$this->instance][$this->A_message][$type] = $value;
}
//Get messages from the session.
public function get_message() {
return $_SESSION[$this->instance][$this->A_message];
}
//Unset messages from the session.
public function unset_message() {
#unset($_SESSION[$this->instance][$this->A_message]);
}
}
c_operations.php
class operations {
//Display all pending messages.
public function display_pending_messages() {
if(session::get_message() != null) {
foreach(session::get_message() as $type => $value) {
if(strlen($type) != null) {
echo '
<div class="panel ' . $type . '">
' . $value . '
<span class="close" onclick="hide_parent_element(this);">X</span>
</div>
';
}
}
session::unset_message();
}
}
}
example.php
$session->set_message('confirm', 'THIS IS A CONFIRM');
$session->set_message('error', 'THIS IS AN ERROR');
$session->set_message('notice', 'THIS IS A NOTICE');
$session->set_message('warning', 'THIS IS A WARNING');
var_dump($_SESSION);
$operation->display_pending_messages();
Errors/notices etc received
Notice: Undefined property: operations::$instance in /var/www/_classes/parent_session.php on line 43
Notice: Undefined property: operations::$A_message in /var/www/_classes/parent_session.php on line 43
Notice: Undefined index: in /var/www/_classes/parent_session.php on line 43
Line 43 refers to the line return $_SESSION[$this->instance][$this->A_message];
Now if I call get_message()
and unset_message()
directly via $session->get_message()
it works as expected but going through another function in this case $operation->display_pending_messages()
it returns the above errors. Obviously this has something to do with the $this->
operator but I'm not to sure on what to do to stop this. I have tried various searches and while finding something similar it wasn't helpful in this case.
Can someone please explain where I've gone wrong and how to fix this?
Thanks in advance.