2014-10-09 229 views
0

我正在处理某人的代码。代码中有一个函数。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(); 
    ............................ 
    ............................ 
} 
?> 

旁注:什么,我想知道的是,当我每次创建新的类实例,那么为什么它不工作?

+2

没有重新声明班级。另外,你最后的努力工作? – sectus 2014-10-09 06:39:06

+0

我不知道该怎么称呼它,你可以编辑标题。我知道在foreach循环中,如果我调用函数,那么它每次都会创建类的新实例。 – DS9 2014-10-09 06:44:28

+0

你最后的努力工作? – sectus 2014-10-09 06:56:31

回答

1

你可以只让APN比如你功能的必需参数和之前的唯一实例循环。

<?php 
$apn = new APN(); 
foreach($alluser as $user) 
{ 
    send_notification($user['device_token'], $apn); 
} 

function send_notification($device_token, APN $apn) 
{ 
    $apn->payloadMethod = 'enhance'; // you can turn on this method for debuggin purpose 
    $apn->connectToPush(); 
    ............................ 
    ............................ 
} 

另一种方法是使用一个单身:

class APN { 
    private static $instance = null; 
    public static function getInstance() { 
     if (null === self::$instance) { 
      self::$instance = new self; 
     } 
     return self::$instance; 
    } 
    //.... whatever your class does 
} 

foreach($alluser as $user) 
{ 
    send_notification($user['device_token'], $apn); 
} 

function send_notification($device_token) 
{ 
    $apn = APN::getInstance(); 
    $apn->payloadMethod = 'enhance'; // you can turn on this method for debuggin purpose 
    $apn->connectToPush(); 
    ............................ 
    ............................ 
} 

艰难的注意,Singleton pattern还带有其缺点一样紧耦合,这使得测试更加努力,也隐藏依赖关系:

Singleton Antipattern

所以我的建议将是第一种方法。

+0

感谢您的回答。它是有益的。但是我不明白的是,当我每次创建类的新实例时,为什么它不起作用? – DS9 2014-10-09 08:23:33

+0

为此,我们需要知道APN的代码。也许它实现了一些锁定,防止在关闭现有连接之前再次实例化。但在这里,我只是猜测。 – enricog 2014-10-09 08:27:31

+0

[APN LIBRARY](https://github.com/antongorodezkiy/codeigniter-apns/blob/master/application/library/apn.php) – DS9 2014-10-09 08:42:23

1

您可以使用使用对象全局,那么你不需要一次又一次地创建对象,例如

<?php 
$apn = new APN(); 
foreach($alluser as $user) 
{ 
    send_notification($user['device_token']); 
} 

function send_notification($device_token) 
{ 
    global $apn; 
    $apn->payloadMethod = 'enhance'; // you can turn on this method for debuggin purpose 
    $apn->connectToPush(); 
    ............................ 
    ............................ 
} 
?> 
+0

请不要使用全局变量,如果涉及到代码的调试和理解,它们是非常糟糕的。 – enricog 2014-10-09 08:06:12