我正在尝试制定最合适的设计来在 PHP 5.3 中的类之间传递会话密钥。
会话密钥是从第 3 方 API 检索的,我的应用程序进行各种 API 调用,所有这些调用都需要传入此会话密钥。
我创建了类来保存相关的 API 调用 egClass cart 保存的方法在调用时会触发对 API 的请求,以从 API_GetCart()、API_AddItem() 等调用返回数据。
我将会话密钥存储在单个 cookie(唯一需要的 cookie)中,并且需要使该 cookie 的值可用于我的几乎所有类。我不能使用数据库或 $_SESSION 来保存会话数据。第 3 方 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 实例,然后将该对象传递给每个需要它的类(几乎都这样做),作为类构造函数的参数或作为方法参数。
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
创建一个 Session 类(实际上只有一个值)是最好的主意吗?考虑到它可能需要保存更多的值并执行更多的检查,让它成为一个类感觉是“正确的”。就将该值传递给各种类而言,最好将 Session 对象传递给它们的构造函数,例如
$se = new Session;
$basket = new Basket($se);
$baskSumm = $basket->getBasketSummary();
我是 OOP 的新手,所以非常感谢一些指导。