Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP OOP :: passing session key between classes
    primarykey
    data
    text
    <p>I am trying to work out the most appropriate design to pass a session key between classes in PHP 5.3.</p> <p>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.</p> <p>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.</p> <p>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.</p> <p>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.</p> <p>One idea I had was to create a Session class like this, and put the session grabbing/checking code in the constructor.</p> <pre><code>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 } } </code></pre> <p>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. </p> <h2> orderSummary.php</h2> <pre><code>$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-&gt;getBasketSummary(); $billing = new Billing; $billSumm = $billing-&gt;getBillingSummary(); $delivery = new Delivery; $delSumm = $delivery-&gt;getDeliverySummary(); //code to render as HTML the customer's basket details //as well as their billing and delivery details </code></pre> <p>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.</p> <pre><code>$se = new Session; $basket = new Basket($se); $baskSumm = $basket-&gt;getBasketSummary(); </code></pre> <p>I'm new to OOP so some guidance would be very much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload