Ich habe folgenden Code:
Wie man sieht wird
zurückgegeben wenn die Sache erfolgreich war. Wenn eine Exception auftritt, gibt es keinen Rückgabewert. Beim debuggen (ich verwende NetBeans mit xDebugger) ist
dann nirgends zu finden aber
gibt
aus.
Funktionieren tut es so, aber ist es auch korrekt? Oder müsste ich im catch explizit 'return false' schreiben?
PHP-Code:
private function _insertGroupIdAndSizeIntoDB($index) {
$fileId = $this->DbData[$index]['Id'];
$groupId = $this->DbData[$index]['groupId'];
$fileSize = $this->DbData[$index]['size'];
try {
$this->PDO->beginTransaction();
$updateCommand = $this->PDO->prepare('UPDATE `file` SET groupId = :groupId, filesize = :size WHERE fileId = :fileId;');
$updateCommand->bindParam(':groupId', $groupId);
$updateCommand->bindParam(':size', $fileSize);
$updateCommand->bindParam(':fileId', $fileId);
$updateCommand->execute();
return true;
} catch (PDOException $ex) {
// aktive Transaktion rückgängig machen
if ($this->PDO->inTransaction()) {
$this->PDO->rollBack();
}
file_put_contents(self::$logFile, $ex->getMessage().' Vorgang wird abgebrochen!'."\r\n", FILE_APPEND);
$this->hasError = true;
}
}
$result = $this->_insertGroupIdAndSizeIntoDB(1);
if ($result) {
echo 'Erfolg!';
}
// der Wert von $index ist für die Frage nicht relevant
PHP-Code:
true
PHP-Code:
$result
PHP-Code:
var_dump($result)
PHP-Code:
null
Funktionieren tut es so, aber ist es auch korrekt? Oder müsste ich im catch explizit 'return false' schreiben?
Kommentar