Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I store object class into a session in PHP?

Tags:

php

How can I have an object class store into PHP session and then get it in my next page as variable. Could you help?

Here is my class.inc.php

class shop {

var $shoeType;
var $color;

    public function __construct() {

        $shoeTypeService = new ShoeTypeService();
        $shoe = $shoeTypeService->getAllShoes();
        $this->shoeType = $shoe[20];
    }
}
like image 805
mary Avatar asked Apr 07 '11 09:04

mary


People also ask

Why session_start () is used in PHP?

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie. When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers.

What should I store in session PHP?

A session is a way to store information (in variables) to be used across multiple pages. Unlike a cookie, the information is not stored on the users computer.

What is stored in $_ session?

The PHP session which is accessible via the global variable $_SESSION is stored on the server as files by default. Also the reference to it (called session_id ) is stored on client side as browser cookies. If either of this is deleted, then the session becomes invalid.

What is the use of session_start () and Session_destroy () functions in PHP?

session_destroy() function: It destroys the whole session rather destroying the variables. When session_start() is called, PHP sets the session cookie in browser. We need to delete the cookies also to completely destroy the session. Example: This example is used to destroying the session.


Video Answer


1 Answers

Once you instantiate the class you can assign it to the session (assuming it's started)

$_SESSION['SomeShop'] = new Shop();

or 

$Shop = new Shop();
//stuff
$_SESSION['SomeShop'] = $Shop;

Keep in mind that wherever you access that object you will need the Shop Class included.

like image 81
Jake Avatar answered Sep 30 '22 05:09

Jake