I am using an array to log key information in a foreach statement for use later on, I set the array at the top of the page to ensure that it is available across all functions in the script.
I have tested to see that the array is set, and it seems to work outside a try catch statement see commented code below.
$completeClients = array();
if($can_synchronise === true)
{
$success = sync_to_client($package, $client);
try
{
if($success)
{
$completeClients[] = "Sync to ".$client->getName()." has completed";
}
else
{
$completeClients[] = "Sync to ".$client->getName()." has failed";
}
}
catch(Exception $ex)
{
logMsg("Unable to save client data reason: ". $ex->getMessage(), STATUS_ERROR , $client->getAddress());
}
exit( EXIT_OK );//Exit the child process
}
}
else
{
**// The array is set correctly when called here**
$completeClients[] = "Sync to ".$client->getName()." has failed";
}
I have printed the array and when it is called in the try catch statement it looks like:
Array
(
[0] => Sync to TPSDEV_PHILIPS_TWO has completed
)
Array
(
[0] => Sync to TPSDEV_TC_Client2 has completed
)
Array
(
)
It should look like
Array
(
[0] => Sync to TPSDEV_PHILIPS_TWO has completed
)
Array
(
[0] => Sync to TPSDEV_PHILIPS_TWO has completed
[1] => Sync to TPSDEV_TC_Client2 has completed
)
Array
(
[0] => Sync to TPSDEV_PHILIPS_TWO has completed
[1] => Sync to TPSDEV_TC_Client2 has completed
)
Do you guys have any idea? im stumped.
This doesn't add up:
exit( EXIT_OK );
It's outside of the try-catch block, so whatever happens (succesful or otherwise), after inserting the first value in the array, you're exiting the script. As soon as the branch if ($can_synchronise === true)
gets executed once.
What's more, there's no loop to be seen anywhere, so how do you expect this to produce an array that contains more than 1 value?
There's something else, too: you're calling the sync_to_client
outside of the try-catch block, which only contains an if-else
and an assignment to an array. Nothing that might throw an exception AFAIK, unless $client->getName()
throws, but the name implies a getter, which is unlikely to throw in the first place.
You probably want to call the sync_to_client
in the try-catch, if that's the function that is likely to throw an exception.
Lastly, if this is part of a loop of some sort, and you've posted just the inner code, then you really ought to move this:
$completeClients = array();
out of the loop, because now, each time this block of code gets executed, you're reassigning $completeClients
to hold a new, empty array.
Assuming an array of client objects, your code should look something like this:
$completeClients = array();
foreach($clients as $client)
{
if (!$canSynchronize)
{
$completeClients[] = 'Sync to '.$client->getName().' has failed';
continue;//messy, best wrap the try-catch in an else branch
}
try
{
if (sync_to_client($package, $client))
{
$completeClients[] = 'Sync to '.$client->getName().' has completed';
}
else
{
$completeClients[] = 'Sync to '.$client->getName().' has failed';
}
}
catch(Exception $e)
{
logMsg("Unable to save client data reason: ". $ex->getMessage(), STATUS_ERROR , $client->getAddress());
//$completeClients[] = 'Unable to Sync: '.$e->getMessage();
//exit( EXIT_OK);
throw $e;//rethrow, is what I'd do.
}
}
However, what this code effectively does is catch exceptions, and sort-of hush them up. If something fails, there's likely to be a bug in your code. Don't catch an exception unless you know how to deal with it. And don't catch everything.PDO
throws PDOException
instances. SOAP clients throw SoapFault
's, my objects throw either InvalidArgumentException
, RuntimeException
or BadMethodCallException
instances. There are many types, each one of them signals a specific type of problem. Don't try to be yoda, playing pokemon
I don't know if mentioned code placed inside an array or function, however I think if you replace following with something else it can help
$completeClients = array();
with
if (!isset($completeClients)) {
$completeClients = array();
}