PHP克隆对象会产生多个对象吗?
首先,我显示代码:
//item duplication code goes here
elseif($_REQUEST['action'] == 'duplicate'){
$array_index = $_REQUEST['array_index'];
$cart = $_SESSION['cart'];
$tempItem = clone $cart[$array_index];
$cart[]=$tempItem;
$_SESSION['cart'] = $cart;
}
例如,如果我的购物车中有 1 件商品,按链接将给我 2 件相同的商品。事实是,直接结果是正确的(我在新选项卡中打开链接)但是在刷新页面(旧选项卡)后,它给了我 3 。
如需解释,请观看一段简短视频:
http://www.youtube.com/watch?v= OORcT5KxZqw
我真的不明白为什么会发生这种情况。任何帮助表示赞赏!
First I show the code:
//item duplication code goes here
elseif($_REQUEST['action'] == 'duplicate'){
$array_index = $_REQUEST['array_index'];
$cart = $_SESSION['cart'];
$tempItem = clone $cart[$array_index];
$cart[]=$tempItem;
$_SESSION['cart'] = $cart;
}
For example, if I have 1 item in the shopping cart, pressing the link will give me 2 identical ones. The fact is, the immediate result is correct(I open the link in new tab) But after I refresh the page(old tab), it gives me 3 .
To explain, please see a short video:
http://www.youtube.com/watch?v=OORcT5KxZqw
I really don't understand why it's happening. Any help is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您对代码所做的事情是,如果
action
的$_REQUEST
等于值duplicate
那么您将执行以下操作:这里发生的情况是仅当您单击重复项时,克隆说明才有效 关联。我想您的 PHP 与 html 位于同一个文件中,因此当您在新窗口中打开它时,购物车会更新 2 个项目并存储在会话中(这对于页面的所有实例都是通用的),并且当您重新加载页面时从会话中获取相同的 2 个项目并向您显示。另外,在这里您需要观察
$_REQUEST['action']
仍然存在,因此它又重复了 1 个项目。现在,每当您刷新页面时,它都会继续复制 1 个项目。作为此问题的解决方案,请尝试在克隆购物车后使用
unset()
函数取消操作,以便 php 仅在需要时运行。What you are doing with your code is that if a
$_REQUEST
ofaction
equals valueduplicate
then you are doing the following things:What is happening here is that the cloning instructions should be valid only when you click the duplicate link. I suppose you have your PHP in the same file as the html so when you open it in a new window the cart is updated with 2 items and stored in session (which is common to all instances of your page) and when you reload your page the same 2 items are fetched from the session and showed to you. Also here you need to observe that the
$_REQUEST['action']
is still present so its duplicating 1 item more. Now whenever you refresh the page it will keep on duplicating 1 item more.As a solution to this try unsetting the action by using the
unset()
function after you have cloned the cart so that the php runs only when needed.我怀疑当您刷新页面时它会给您三个,因为旧的
$_REQUEST['array_index']
仍然存在。您需要设置一个会话变量来指示操作已完成,并在继续执行此操作之前检查它。I suspect it is giving you three when you refresh the page because the old
$_REQUEST['array_index']
is still hanging around. You need to set a session variable to indicate that the action has been completed, and check it before proceeding to do this action.由于 $_REQUEST 也包含 $_GET,我猜你的
&action=duplicate
位于url 和您的代码会在您重新加载页面时执行。我建议重定向(使用
header('Location: ...');
,请参阅:header) 在复制之后,但仅在从 URL 中删除action
-var 之后。更新:
使用函数getUrlCurrently这里有一个简短的例子:
As $_REQUEST also holds $_GET, I'd guess your
&action=duplicate
sits in the url and your code get's executed as often as you reload the page.I would suggest to redirect (using
header('Location: ...');
, see: header) after the duplication, but only after removing theaction
-var from the url.Update:
Using the function getUrlCurrently here's a short example: