PHP code example of mindplay / session

1. Go to this page and download the library: Download mindplay/session library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mindplay / session example snippets


class Cart
{
    /** @var int */
    public $user_id;

    /** @var int[] */
    public $product_ids = array();
}

// commit session container contents to session variables:

$session->commit();

register_shutdown_function(function () use ($session) {
    $session->commit();
});

use mindplay\session\SessionContainer;

$session = new SessionContainer();

// add some products to the Cart:

$session->update(
    function (Cart $cart) {
        $cart->product_ids[] = 777;
        $cart->product_ids[] = 555;
    }
);

$cart = $session->update(function (Cart $cart) {
    return $cart;
});

// empty the cart:

$session->remove(Cart::class);

$session->update(
    function (Cart $cart = null) {
        if ($cart) {
            // ...
        }
    }
);

$session->update(function (User $user, Cart $cart) {
    // ...
});

use mindplay\session\MockSessionStorage;
use mindplay\session\SessionContainer;

$storage = new MockSessionStorage('foo');

$container = new SessionContainer($storage);