1

I'm new to oop and some lines of my code are only approximate, to indicate what I want to achieve.

I want to create a session with php within a function within a class and call it in another function.

class My_beautiful_class
{
public function index()
    {
        $_SESSION['ciao'] = 'ciao';
        var_dump($_SESSION);
    }
public function anotherfunction()
    {
        $this->index();
        var_dump($_SESSION);
    }
}

I just want to understand a concept: in this way, my code will work but, on the other end, it will execute everything it will find in my function index, in my other function anotherfunction. So, if I call two variable with the same name, I could have a problem.

I guess that in theory, I could handle the problem in another way which is that I could create a variable call sessionone for example and send it some values in with my index function:

class My_beautiful_class
{

public sessionone = [];

public function index()
    {
        $_SESSION['ciao'] = 'ciao';
        $this->sessionone = $_SESSION['ciao'];

    }
public function anotherfunction()
    {
        $this->sessionone;
        var_dump($_SESSION);
    }
}

, but I was wondering if there any way to for example call only one variable that lives in one function with my first approach.

Something like: (My code is wrong on purpose, it is only to indicate what I want to achieve)

public function index()
        {
            $_SESSION['ciao'] = 'ciao';
        }
public function anotherfunction()
        {
           $this->index( $_SESSION['ciao'] );
        }
    }
3
  • Once you create a session, it's available anywhere, $_SESSION has a global scope. Commented May 30, 2018 at 19:42
  • It was actually my problem. $_SESSION['ciao'] = 'ciao'; doesn't work in another function because they are in two different pages. But if I create a variable outside the function it actually works Commented May 30, 2018 at 19:46
  • I actually don't understand what you're trying to achieve, and the problem that you're trying to solve here. Commented May 30, 2018 at 19:53

2 Answers 2

5

The $_SESSION variable are what is called a Superglobal in PHP: http://php.net/manual/en/language.variables.superglobals.php This gives them a few unique traits.

First, Superglobals are accessible anywhere in your application regardless of scope. Setting the value of a superglobal key in one function will make the value accessible anywhere else in your application that you want to reference it.

Say for example we want to make a class to manage our session. That class may look something like this.

class SessionManager 
{
    public function __construct()
    {
        session_start();   //We must start the session before using it.
    }

    //This will set a value in the session.
    public function setValue($key, $value)
    {
        $_SESSION[$key] = $value;
    }

    //This will return a value from the session
    public function getValue($key)
    {
        return $_SESSION[$key];
    }

    public function printValue($key)
    {
        print($_SESSION[$key]);
    }
}

Once we have a class to manage it a few things happen. We can use this new class to add info to the session and also to retrieve it.

Consider the following code:

$session = new SessionManager();
$session->setValue('Car', 'Honda');

echo $session->getValue('Car'); // This prints the word "Honda" to the screen.
echo $_SESSION['Car']; //This also prints the word "Honda" to the screen.
$session->printValue('Car'); //Again, "Honda" is printed to the screen.

Because of a session being a superglobal, once you set a value on the session it will be assessable anywhere in your application, either in or outside of the class itself.

Sign up to request clarification or add additional context in comments.

1 Comment

As long as you always create the SessionManager class on each page load, you will be able to access any set session variables between pages. But you will have to create the class before you want to retrieve any saved values from the session.
1

You must call session_start() at the beginning of your class. You can also start the session at the beginning of your code if you include your class like this sample

Remember that you can call the session_start only one time

2 Comments

It's in french, where should I start the session? Inside a function? Inside a construct? inside a variable?
Yes, sorry for this french topic. You need to use session_start at the entry point of your application to create the session only one time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.