PHP OOP :: 在类之间传递会话密钥
我正在尝试找出最合适的设计来在 PHP 5.3 中的类之间传递会话密钥。
会话密钥是从第 3 方 API 检索的,我的应用程序进行各种 API 调用,所有调用都需要传入此会话密钥。
我创建了类来保存相关的 API 调用,例如 Class cart 保存的方法在被调用时将触发请求 API 从 API_GetCart()、API_AddItem() 等调用返回数据。
我将会话密钥存储在单个 cookie 中(唯一需要的 cookie),并且需要将该 cookie 的值提供给几乎我所有的课程。我无法使用数据库或 $_SESSION 来保存会话数据。第 3 方 API 负责诸如购物篮内容等的会话管理。
当用户第一次访问我的应用程序时,不会有 cookie 值,因此我需要能够为新 cookie 分配新的会话密钥并将该值(由于我们仍在处理相同的 HTTP 请求,因此还不能作为 cookie 使用)传递给其他类。
我的一个想法是创建一个像这样的 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 新手,因此非常感谢一些指导。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用工厂模式。 Basket、Billing 和 Delivery 对象应由第 3 方服务 API 包装类创建:将
Basket、Billing 和 Delivery 类与 API 连接的最佳方式是存储对 API 类的引用,然后它们可以调用它的任何方法,不仅仅是 getSession()。
另一个优点是,如果您有一个已识别的实体,例如用户,那么包装类可以授予您场景中不会有双重对象的权利。
如果主程序创建用户,那么同一个用户应该有不同的对象,这是错误的:
VS如果API包装器创建它们,包装器类应该保留用户的“缓存”,并返回相同的User对象相同的请求:(
也许这不是一个最好的例子,如果一个程序创建了两次相同的用户,则意味着该程序有重大设计错误。)
更新: svc 类的解释
The3rdPartyServiceApiWrapper 应该看起来像这个:
篮子:
You may use Factory Pattern. Basket, Billing and Delivery objects should be created by the 3rd-party service API wrapper class:
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:
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:
(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:
The Basket: