简体   繁体   中英

Call a session inside a function with object oriented php

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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