Autor Beitrag
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Di 08.05.07 17:57 
Wenn man eine php-Funktion hat, deren Rückgabewert boolean ist und dieser den Wert false annimmt, dann möchte ich, dass dementsprechend auch gleich die dazugehörige Fehlermeldung als string abgerufen werden kann. Bis jetzt mache ich das so:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
public function DeletePage($id,&$errormsg) {
  ...
  if (!$result) {
     $errormsg = "bla";
     return false;
  }
  else return true;
}

Geht das auch einfacher?


Moderiert von user profile iconKlabautermann: Topic aus Off Topic verschoben am Mi 09.05.2007 um 17:18

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Di 08.05.07 18:28 
Ich kenn nur eine leicht andere Methode:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
function XYZ() {
  global $errormsg;
  $errormsg = 'Blup';
  Return False;
}

// ... //

$errormsg = '';
if (XYZ() === false)
  echo($errormsg);

Das global macht die Variable in der Funktion bekannt. Ist ein wenig einfacher, als die Variable zu übergeben.

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
blackbirdXXX

ontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 1077
Erhaltene Danke: 1

Ubuntu Dapper

BeitragVerfasst: Di 08.05.07 18:28 
Titel: Re: [php]Rückgabewert boolean => wenn false, dann zus. Error
user profile iconMarco D. hat folgendes geschrieben:
Wenn man eine php-Funktion hat, deren Rückgabewert boolean ist und dieser den Wert false annimmt, dann möchte ich, dass dementsprechend auch gleich die dazugehörige Fehlermeldung als string abgerufen werden kann. Bis jetzt mache ich das so:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
public function DeletePage($id,&$errormsg) {
  ...
  if (!$result) {
     $errormsg = "bla";
     return false;
  }
  else return true;
}

Geht das auch einfacher?


Es geht zumindest eleganter:

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
<?php

class ApplicationException extends Exception {};

function handle_application_exception($exception) {
  if ($exception instanceof ApplicationException) {
    include 'template/application_error.php';
  }
  else {
    /* handle generic errors here */
  }
}

set_exception_handler('handle_application_exception');

class Page {
  public function Delete() {
    ...
    if (!$result)
      throw new ApplicationError('...');
    return $result;
  }
}

$mypage = new Page(42);
try {
  $mypage->Delete();
}
catch (ApplicationError $e) {
  ...
}


Wenn der Fehler nicht gefangen wird gehts an handle_application_exception weiter.

_________________
Klein, schwarz und ärgert Techniker? Jumper!
Marco D. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Mi 09.05.07 17:16 
@blackbird:
Warum genau ist deine Variante eleganter? Welche Vorteile hat sie bzw. welche Nachteile hat meine Variante? Warum sollte ich gerade Exceptions verwenden?

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
blackbirdXXX

ontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 1077
Erhaltene Danke: 1

Ubuntu Dapper

BeitragVerfasst: Mi 09.05.07 17:41 
user profile iconMarco D. hat folgendes geschrieben:
@blackbird:
Warum genau ist deine Variante eleganter? Welche Vorteile hat sie bzw. welche Nachteile hat meine Variante? Warum sollte ich gerade Exceptions verwenden?


Weil die Dinger genau dafür entwickelt wurden und PHP sie Gott sei Dank nach 10 Jahren eingebaut hat.

_________________
Klein, schwarz und ärgert Techniker? Jumper!
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mi 09.05.07 18:04 
user profile iconblackbirdXXX hat folgendes geschrieben:
user profile iconMarco D. hat folgendes geschrieben:
@blackbird:
Warum genau ist deine Variante eleganter? Welche Vorteile hat sie bzw. welche Nachteile hat meine Variante? Warum sollte ich gerade Exceptions verwenden?


Weil die Dinger genau dafür entwickelt wurden und PHP sie Gott sei Dank nach 10 Jahren eingebaut hat.
"Es gibt sie, also muss man sie benutzen"? Das ist keine Antwort auf die Frage. "Die Dinger" sind kein Selbstzweck. Welchen Vorteil bieten sie hier?

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Marco D. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Mi 09.05.07 21:43 
Ich gebe Christian Recht :!: Du hast die Frage nicht beantwortet. ;) Was bringt mir sowas. Meine Variante ist bedeutend kürzer. ;)

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
blackbirdXXX

ontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 1077
Erhaltene Danke: 1

Ubuntu Dapper

BeitragVerfasst: Do 10.05.07 00:06 
Der Vorteil von Exceptions ist, dass man sie nicht immer direkt fangen muss. In komplexeren Anwendungen gibt es tausende Stellen wo Fehler auftreten können. Viele fang ich dann einfach nicht und lasse sie (in PHP von einem Error Handler) bzw in Ruby und Python von einer WSGI/Rack Middleware abfangen, mir eine E-Mail zustellen und dem Benutzer eine vereinfachte Fehlermeldung präsentieren.

Fehler vom Type `ApplicationException` oder soetwas in der Richtung bekommen dann keinen Mailversand weil sie bestimmte gewollte Zustände der Anwendung präsentieren (zb Redirect auf andere URL, Forumlardaten nicht komplett ausgefüllt etc).

_________________
Klein, schwarz und ärgert Techniker? Jumper!
Marco D. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Do 10.05.07 13:57 
@blackbird:
Wie genau funktioniert das mit dem Error Handler in PHP?

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
blackbirdXXX

ontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 1077
Erhaltene Danke: 1

Ubuntu Dapper

BeitragVerfasst: Do 10.05.07 16:36 
user profile iconMarco D. hat folgendes geschrieben:
@blackbird:
Wie genau funktioniert das mit dem Error Handler in PHP?


lucumr.pocoo.org/tra...mples/errorhandling/

_________________
Klein, schwarz und ärgert Techniker? Jumper!