DS9
DS9

Reputation: 3033

Re-instantiation of an object inside foreach loop

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?

Upvotes: 0

Views: 874

Answers (2)

enricog
enricog

Reputation: 4273

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.

Upvotes: 1

Sagar Rabadiya
Sagar Rabadiya

Reputation: 4321

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

Upvotes: 1

Related Questions