[英]PHP OOP :: passing session key between classes
我試圖找出最合適的設計來在PHP 5.3中的類之間傳遞會話密鑰。
會話密鑰是從第三方API檢索的,我的應用程序會進行各種API調用,這些調用都需要傳入此會話密鑰。
我已創建了用於保存相關API調用的類egClass cart保存方法,這些方法在調用時將觸發對API的請求,以從API_GetCart(),API_AddItem()等調用返回數據。
我將會話密鑰存儲在一個cookie(唯一需要的cookie)中,並且需要使該cookie的值可用於我的所有類。 我不能使用數據庫或$ _SESSION來保存會話數據。 第三方API負責管理籃子內容等會話管理。
當用戶第一次到達我的應用程序時,將沒有cookie值,因此我需要能夠為新cookie分配新的會話密鑰並傳遞該值(因為我們是仍然處理相同的HTTP請求)到其他類。
我的一個想法是創建一個像這樣的Session類,並將會話抓取/檢查代碼放在構造函數中。
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
}
}
然后在所有視圖頁面上,我將實例化一個新的Session實例,然后將該對象傳遞給需要它的每個類(幾乎所有這些),作為類構造函數的參數或作為方法參數。
$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
創建一個Session類(實際上只包含一個值)是最好的主意嗎? 鑒於它可能需要持有更多的價值並進行更多的檢查,因此感覺“正確”使它成為一個階級。 就將該值傳遞給各個類而言,最好將Session對象傳遞給它們的構造函數,例如
$se = new Session;
$basket = new Basket($se);
$baskSumm = $basket->getBasketSummary();
我是OOP的新手,所以非常感謝一些指導。
您可以使用工廠模式。 Basket,Billing和Delivery對象應由第三方服務API包裝類創建:
$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();
將Basket,Billing和Delivery類與API連接的最佳方法是存儲對API類的引用,然后它們可以調用它的任何方法,而不僅僅是getSession()。
另一個優點是,如果你有一個已識別的實體,例如一個用戶,那么包裝類可以授予你,場景中不會有雙重對象。
如果主程序創建用戶,則應該有相同用戶的不同對象,這是錯誤的:
$user1 = new User("fred12");
$user2 = new User("fred12");
VS如果API包裝器創建它們,則包裝器類應該保留用戶的“緩存”,並使用相同的User對象返回相同的請求:
$user1 = $svc->createUser("fred12");
$user2 = $svc->createUser("fred12"); // $user2 will be the same object
(也許這不是一個最好的例子,如果一個程序創建兩個相同的用戶,則意味着該程序的主要設計有問題。)
更新: svc類的解釋
The3rdPartyServiceApiWrapper應如下所示:
function getSessionId() {
return $this->sessionId; // initialized by constructor
} // getSessionId()
function createBasket() {
$basket = new Basket($this);
return $basket;
} // createBasket()
籃子:
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.