0

I am working on someone's code. there is one function in code.

it is like following:

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

then inside foreach loop, he call the function.

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

now if i run above code then it says APN Failed to connect: Something wrong with context.

so i changed few things in code.

$apn = new APN();
foreach($alluser as $user)
{
    $apn->payloadMethod = 'enhance'; // you can turn on this method for debuggin purpose
    $apn->connectToPush();
    ............................
    ............................
}

i create the object of class outside of foreach loop then it is working.
but the wrost thing is, i have to write above code in each place (this page contain other foreach).

So how can i solve above problem in a smart way?

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();
    ............................
    ............................
}
?>

Sidenote: What i am trying to know is when i create new instance of class each time then why it is not working?

DS9
  • 2,831
  • 4
  • 40
  • 91

2 Answers2

1

you can use the object using global then you don't need to create objects again and again for example

<?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();
    ............................
    ............................
}
?>
Sagar Rabadiya
  • 3,740
  • 1
  • 17
  • 27
  • Please never use globals, they are a are just awful if it comes to debugging and understanding of the code. – enricog Oct 09 '14 at 08:06
1

You could just make the APN instance a required parameter of your function and only instantiate before the loop.

<?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();
    ............................
    ............................
}

Another approach would be to use a singleton:

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();
    ............................
    ............................
}

Tough note that the Singleton pattern also comes with its downsides like tight coupling which makes testing harder and also hide dependencies:

Singleton Antipattern

So my suggestion would be the first approach.

Community
  • 1
  • 1
enricog
  • 3,964
  • 5
  • 30
  • 49
  • thanks for your answer. it is useful. But what I don't understand is when I create a new instance of the class each time then why it is not working? – DS9 Oct 09 '14 at 08:23
  • For this we would need to know the code of the APN. Maybe it implements some locking which prevents to instantiate a second time before closing the existing connection. But here I am just guessing. – enricog Oct 09 '14 at 08:27
  • [APN LIBRARY](https://github.com/antongorodezkiy/codeigniter-apns/blob/master/application/library/apn.php) – DS9 Oct 09 '14 at 08:42
  • Seems the lib uses phps `stream_socket_client` to create the connection and that it fails on connecting. Maybe because you are connecting several times you have too many connections opened at some point, which may cause the error. Tough, i don't have any experience with it, so I think I can't help you any further. – enricog Oct 09 '14 at 08:56
  • ok.. no problem. any way you solve my problem. Thanks for that. – DS9 Oct 09 '14 at 08:57