首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP OOP ::在类之间传递会话密钥

PHP OOP ::在类之间传递会话密钥
EN

Stack Overflow用户
提问于 2011-01-04 17:19:38
回答 1查看 1.3K关注 0票数 4

我正在尝试设计出最合适的设计来在PHP5.3中的类之间传递会话密钥。

会话密钥是从第三方API检索的,我的应用程序进行各种API调用,这些API调用都需要传入此会话密钥。

我已经创建了类来保存相关的API调用,e.g.Class cart保存的方法,当被调用时,将触发对API的请求,以从API_GetCart(),API_AddItem()等调用中返回数据。

我将会话密钥存储在单个cookie中(唯一需要的cookie ),并且需要使该cookie的值对我的几乎所有类都可用。我不能使用数据库或$_SESSION来保存会话数据。第三方API负责会话管理,如篮子内容等。

当用户第一次访问我的应用程序时,将没有cookie值,因此我需要能够将新的会话密钥分配给新的cookie,并将该值(尚未作为cookie提供,因为我们仍在处理相同的HTTP请求)给其他类。

我的一个想法是创建一个像这样的会话类,并将会话抓取/检查代码放在构造函数中。

代码语言:javascript
复制
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

代码语言:javascript
复制
$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 

创建一个会话类(实际上只包含一个值)是不是最好的主意?考虑到它可能需要保存更多的值并执行更多的检查,让它成为一个类感觉是“正确的”。就将该值传递给各个类而言,最好是将会话对象传递给它们的构造函数,例如

代码语言:javascript
复制
$se = new Session;
$basket = new Basket($se);
$baskSumm = $basket->getBasketSummary();

我是OOP的新手,所以如果能给我一些指导,我将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-01-04 17:46:32

您可以使用工厂模式。购物篮、计费和交付对象应由第三方服务API包装器类创建:

代码语言:javascript
复制
$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();

将购物篮、账单和交付类与应用程序接口连接的最好方法是存储对应用程序接口类的引用,然后他们可以调用它的任何方法,而不仅仅是getSession()。

另一个优点是,如果你有一个被识别的实体,例如一个用户,那么包装器类可以允许你,在场景中不会有双重对象。

如果主程序创建了用户,应该有不同的对象具有相同的用户,这是错误的:

代码语言:javascript
复制
$user1 = new User("fred12");
$user2 = new User("fred12");

VS如果API包装器创建了它们,包装器类应该保留用户的“缓存”,并为相同的请求返回相同的User对象:

代码语言:javascript
复制
$user1 = $svc->createUser("fred12");
$user2 = $svc->createUser("fred12");  // $user2 will be the same object

(也许这不是一个最好的例子,如果一个程序创建了两个相同的用户,这意味着该程序存在重大的设计缺陷。)

更新: svc类的解释

The3rdPartyServiceApiWrapper应如下所示:

代码语言:javascript
复制
 function getSessionId() {
   return $this->sessionId;  // initialized by constructor
 } // getSessionId()

 function createBasket() {
   $basket = new Basket($this);
   return $basket;
 } // createBasket()

篮子:

代码语言:javascript
复制
 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()
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4592081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档