Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3624348
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:29:47+00:00 2026-05-18T23:29:47+00:00

I am trying to work out the most appropriate design to pass a session

  • 0

I am trying to work out the most appropriate design to pass a session key between classes in PHP 5.3.

The session key is retrieved from a 3rd-party API and my application makes various API calls that all require this session key to be passed in.

I have created classes to hold related API calls e.g.Class cart holds methods that, when called, will fire off request to the API to return data from calls such as API_GetCart(), API_AddItem() etc.

I am storing the session key in a single cookie (the only cookie that is ever required) and need to make the value of that cookie available to pretty much all of my classes. I cannot use a database or $_SESSION to hold session data. The 3rd-party API looks after session management for things like basket contents etc.

When a user reaches my app for the very first time there will be no cookie value so I need to be able to both assign a new session key to a new cookie and also pass that value (not yet available as a cookie since we’re still processing the same HTTP request) to the other classes.

One idea I had was to create a Session class like this, and put the session grabbing/checking code in the constructor.

class Session {
    public $sk;
    function __construct() {
        //code to check if user has sessionkey (sk) in cookie
        //if not, grab new sessionkey from 3rd party API and assign to new cookie
        // $sk = 'abcde12345'; //example $sk value
    }
}

Then on all view pages I would instantiate a new instance of Session and then pass that object into each class that requires it (nearly all do), either as argument to class constructor or as method argument.

orderSummary.php

$s = new Session;

//$s currently would only hold one variable, $sk = "abcde12345"
//but in the future may hold more info or perform more work

// what is best approach to making the sessionkey 
// available to all classes? arg to constructor or method... or neither :)

$basket = new Basket;
$baskSumm = $basket->getBasketSummary();

$billing = new Billing;
$billSumm = $billing->getBillingSummary();

$delivery = new Delivery;
$delSumm = $delivery->getDeliverySummary();

//code to render as HTML the customer's basket details
//as well as their billing and delivery details 

Is creating a Session class (that really only holds a single value) the best idea? Given that it may need to hold more values and perform more checking, it felt ‘right’ making it a class. In terms of passing that value to the various classes, would it be best to pass in the Session object to their constructor, e.g.

$se = new Session;
$basket = new Basket($se);
$baskSumm = $basket->getBasketSummary();

I’m new to OOP so some guidance would be very much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T23:29:48+00:00Added an answer on May 18, 2026 at 11:29 pm

    You may use Factory Pattern. Basket, Billing and Delivery objects should be created by the 3rd-party service API wrapper class:

    $svc = new The3rdPartyServiceApiWrapper();
    $svc->init();  // connect, get session etc.
    if ($svc->fail()) die("halp! error here!");
    
    $basket = $svc->createBasket();
    $baskSumm = $basket->getBasketSummary();
    
    $billing = $svc->createBilling();
    $billSumm = $billing->getBillingSummary();
    
    $delivery = $svc->createDelivery();
    $delSumm = $delivery->getDeliverySummary();
    

    The best way to connect Basket, Billing and Delivery class with the API is storing a reference to the API class, then they can call any method of it, not just getSession().

    Another advantage is, that if you have an identified entity, e.g. a User, then the wrapper class can grant you, that there will be no double objects in the scene.

    If the main program creates the users, there should be different objects with the same user, which is wrong:

    $user1 = new User("fred12");
    $user2 = new User("fred12");
    

    VS if the API wrapper creates them, the wrapper class should keep a “cache” of users, and return with the same User object for the same request:

    $user1 = $svc->createUser("fred12");
    $user2 = $svc->createUser("fred12");  // $user2 will be the same object
    

    (Maybe it’s not a best example, if a program creates twice the same User, it means that the program has major design faulty.)

    UPDATE: explanation of svc class

    The The3rdPartyServiceApiWrapper should look like this:

     function getSessionId() {
       return $this->sessionId;  // initialized by constructor
     } // getSessionId()
    
     function createBasket() {
       $basket = new Basket($this);
       return $basket;
     } // createBasket()
    

    The Basket:

     function Basket($s) {  // constructor of Basket class
    
       $this->svc = $s;
    
       //... the rest part of constructor
    
     } // Basket() constructor
    
    function doSomethingUseful() {
    
      // if you wanna use the session:
      $sess = $this->svc->getSessionId();
      echo("doing useful with session $session");
    
      // you may access other api functions, I don't know what functions they provide
      $this->svc->closeSession();
    
    } // doSomethingUseful()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to figure out which is most appropriate. From the articles I
I am trying to work out the best way to design a class that
I am trying to work out a way to alter the session timeout dynamically,
I am trying to work out the best database model for the current setup:
I am trying to work out how to get the value of table cell
I'm trying to work out a way of passing the web current http context
I am trying to work out a code sample to demonstrate the debugging functionality
I am trying to work out the format of a password file which is
I'm still working on groking the F# thing - trying to work out how
I found this in an error log and am trying to work out how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.