我正在处理某人的代码。代码中有一个函数。foreach循环中对象的重新实例化
它像以下:
function send_notification($device_token)
{
$apn = new APN();
$apn->payloadMethod = 'enhance'; // you can turn on this method for debuggin purpose
$apn->connectToPush();
............................
............................
}
然后foreach循环里面,他调用该函数。
foreach($alluser as $user)
{
send_notification($user['device_token']);
}
现在,如果我运行上面的代码,那么它说APN Failed to connect: Something wrong with context
。
所以我在代码中改变了一些东西。
$apn = new APN();
foreach($alluser as $user)
{
$apn->payloadMethod = 'enhance'; // you can turn on this method for debuggin purpose
$apn->connectToPush();
............................
............................
}
我创建foreach循环之外的类的对象然后它工作。
但事实是,我必须在每个地方编写上面的代码(本页面包含其他foreach)。
那么我怎么才能以一种聪明的方式解决上述问题?
FULL CODE (Just some part)
<?php
foreach($alluser as $user)
{
send_notification($user['device_token']);
}
function send_notification($device_token)
{
$apn = new APN();
$apn->payloadMethod = 'enhance'; // you can turn on this method for debuggin purpose
$apn->connectToPush();
............................
............................
}
?>
旁注:什么,我想知道的是,当我每次创建新的类实例,那么为什么它不工作?
没有重新声明班级。另外,你最后的努力工作? – sectus 2014-10-09 06:39:06
我不知道该怎么称呼它,你可以编辑标题。我知道在foreach循环中,如果我调用函数,那么它每次都会创建类的新实例。 – DS9 2014-10-09 06:44:28
你最后的努力工作? – sectus 2014-10-09 06:56:31