PHP code example of icehawk / session

1. Go to this page and download the library: Download icehawk/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/ */

    

icehawk / session example snippets


 declare(strict_types=1);

namespace MyVendor\MyProject;

use IceHawk\Session\AbstractSession;

final class Session extends AbstractSession
{
    const KEY_SOME_STRING_VALUE = 'someStringValue';
    
    public function getSomeStringValue() : string
    {
        return $this->get( self::KEY_SOME_STRING_VALUE );
    }

    public function setSomeStringValue( string $value )
    {
        $this->set( self::KEY_SOME_STRING_VALUE, $value );
    }

    public function isSomeStringValueSet() : bool
    {
        return $this->isset( self::KEY_SOME_STRING_VALUE );
    }

    public function unsetSomeStringValue()
    {
        $this->unset( self::KEY_SOME_STRING_VALUE );
    }
}

 declare(strict_types=1);

namespace MyVendor\MyProject;

$session = new Session( $_SESSION );

# Set
$session->setSomeStringValue( 'Hello world' );

# Isset
if ( $session->isSomeStringValueSet() )
{
    # Get
    $someString = $session->getSomeStringValue();
    
    echo $someString . ' was set.';
}

# Unset 
$session->unsetSomeStringValue();

 declare(strict_types=1);

namespace MyVendor\MyProject;

use IceHawk\Session\Interfaces\MapsSessionData;

# Create a data mapper class
final class MyDataMapper implements MapsSessionData
{
	public function toSessionData( $value ) 
	{
        return base64_encode( $value );
	}
	
	public function fromSessionData( $sessionData ) 
	{
		return base64_decode( $sessionData );
	}
}

$session = new Session( $_SESSION );

# Add the data mapper for all keys in the registry
$session->addDataMapper( new MyDataMapper() );

# Add the data mapper for one specific key in the registry
$session->addDataMapper( new MyDataMapper(), [Session::KEY_SOME_STRING_VALUE] );

# Add the data mapper for multiple keys in the registry
$session->addDataMapper( new MyDataMapper(), [Session::KEY_SOME_STRING_VALUE, Session::KEY_SOME_OTHER_VALUE] );

 declare(strict_types=1);

namespace MyVendor\MyProject;

$session = new Session( $_SESSION );

# ... put some data to the session ...

# Clear the session data
$session->clear();