我正在构建一些常见的类对象,这些对象将被常规地隐藏在 $_SESSION 对象中。我了解到,在将对象添加到会话时,有时可以在不序列化的情况下添加它,但它不可靠,所以我先序列化并且它每次都有效。
我发现以下内容非常乏味/重复和笨重......就像我必须从会话中检查对象做一些工作然后记得以正确的方式检查它。有没有更好/更有创意的方式也有效且可重复使用。帮助php专家
// Painful Code
$tmpObj = new clsSession; // Class setup for intellisense to work.
$tmpObj = unserialize($_SESSION['objSession']); // I assign so i can get intellisense as well.
$tmpObj //... do some work make some changes some other logic could be lengthy...
// and then i have to remember to do the following
$_SESSION['objSession'] = serialize($tmpObj);
unset $tmpObj;
/* or better */
$tmpObj = $_SESSION['objSession'];
$tmpObj = unserialize($tmpObj);
$tmpObj //... do some work make some changes some other logic
//and then i have to remember to do the following
$_SESSION['objSession'] = serialize($tmpObj);
unset $tmpObj;