Photoshop
Cinema 4d
Fotografie
Weitere Grafiksoftware
HTML / CSS
JavaScript
Flash
PHP
Webserver
Sonstige
PHP & OOP - Eigene Assertion-Klasse (PHP Tutorial)
Tutorial erstellt von Andreas W. in 5.x, letzte Änderung am 22.06.2010
In dem nachfolgenden Tutorial möchte ich euch zeigen, wie man die PHP eigene Assertion Funktion überschreibt bzw. neu definiert. Zu diesem Zweck werde ich nicht nur einfach die Klasse vorstellen, sondern auch einige Beispiele aus der Praxis anführen. Die gezeigten Anwendungen und Methoden werde ich dabei ausführlich erläutern.
Wie viele andere Programmiersprachen bietet auch PHP seinem Anwender die Möglichkeit, seine Programme mit Hilfe von Assertions zu debuggen. Die assert()-Funktion überprüft dabei einen übergebenen Term auf seine Korrektheit und gibt eine Nachricht aus, falls dieser nicht erfüllt ist. Es ist dabei auch möglich den Term als String zu übergeben. Dieser wird dann als PHP-Code ausgeführt. Die Verwendung einer String-Assertion hat den Vorteil, dass sie weniger Arbeit machen, falls Assertions deaktiviert sind, und außerdem ist es so möglich den Assertion-Code auszugeben, falls dieser mit false evaluiert wird. Das bedeutet, dass ein Boolescher Ausdruck, der an die assert() Funktion übergeben wird, nicht als weiterer Parameter an die via assert_options() definierte Funktion übergeben wird. Es wird also nur ein leerer String anstatt des Terms ausgegeben.
Wie sich bereits vermuten lässt, falls es nicht so oder so schon bekannt ist, ist die Verwendung von Assertions nur zum Debuggen von Quellcode und nicht zur Überprüfung von Parametern in einem tatsächlichen Programm gedacht.
PHP bietet einem die Möglichkeit die Funktionsweise der assert() Funktion zu beeinflussen. Dies ist zum einen über entsprechende Einstellungen in den ini-Einstellungen möglich, aber auch, wie ich bereits erwähnte, über die Funktion assert_options(). In beiden Fällen ist es möglich eine Callback Funktion zu definieren, die im Falle einer fehlgeschlagenen Assertion aufgerufen wird.
Diese Callback-Funktion sollte drei Parameter enthalten: die Datei und die Zeile in der die Assertion auftrat sowie optional den Code, der diese verursachte. Diese Informationen kann man relativ einfach via debug_backtrace() abfragen.
Wir wollen nun die Möglichkeit von PHP nutzen, die Funktionsweise von Assertions zu beeinflussen, um eine eigene Assertion-Klasse zu implementieren. Dazu sollten wir uns jedoch als erstes fragen, welche Anforderungen wir an eine solche Klasse stellen wollen.
Die Klasse soll natürlich eine eigene Callback-Methode definieren, die es uns ermöglicht, die Ausgabe unserer Assertions zu beeinflussen. Außerdem soll sie verschiedene Methoden bereitstellen, die entweder übergebene Variablen oder Terme auf ihre Korrektheit prüfen oder diese schlichtweg ausgeben. Des weiteren wäre es schön, wenn es Methoden gäbe, die den Typ einer Variablen ausgeben oder auf einen bestimmten Typ prüfen. Zudem wäre es schön, wenn es möglich wäre optional eigene Fehlermeldungen zu definieren. Zuletzt sollte es noch eine Methode geben, die es ihrem Anwender ermöglicht, einfach nur an einer bestimmten Stelle die Ausführung des Quellcodes zu stoppen und dabei eine Ausgabe zu machen. Es sollen daher die nachfolgenden Methoden implementiert werden.
* areEqual($actual, $expected, $msg = null)
* areNotEqual($actual, $expected, $msg = null)
* arrayEquals($actual, $expected, $msg = null)
* arrayNotEquals($actual, $expected, $msg = null)
* dumpFormatted($value, $msg = null)
* fail($msg = null)
* getType($obj, $msg = null)
* isEmpty($value, $msg = null)
* isFalse($term, $msg = null)
* isInstanceOf($obj, $type, $msg = null)
* isNotEmpty($value, $msg = null)
* isNotInstanceOf($obj, $type, $msg = null)
* isNotNull($value, $msg = null)
* isNull($value, $msg = null)
* isTrue($term, $msg = null)
* toString($value, $msg = null)
* callback($file, $line, $code = null)
Bevor wir jedoch mit der Implementierung dieser Methoden beginnen, sollten wir uns noch über einige Dinge im Klaren werden. Es soll weiterhin möglich sein die assert() Funktion ganz normal zu verwenden. Der Programmablauf soll beim Auftreten einer Assertion nicht unbedingt unterbrochen werden. Die Verwendung von die() oder exit() fallen also aus. Es ist so oder so viel schöner, wenn man Exceptions verwendet. Um diese jedoch getrennt von anderen Ausnahmen behandeln zu können, sollte man eine eigene Ausnahme definieren. Wir werden diese AssertException nennen. Ihre Implementierung sieht wie folgt aus.
Code:
/**
* @package core::debugging
* @class AssertException
*
* The AssertException class implements a
* Exception type for the Assert class.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 24.04.2010<br />
*/
class AssertException extends Exception {}
Zuletzt sollte uns auch klar sein, dass es wenig Sinn macht beliebig viele Instanzen der Assertion Klasse zu erzeugen. Es ist daher sinnvoll, diese als Singleton zu entwerfen.
Nun kann die eigentliche Programmierarbeit beginnen. Die einzelnen Assertion-Methoden haben dabei alle die gleiche Struktur. Als erstes wird geprüft, ob die Bedingung, nach der gefragt wird, erfüllt ist. Ist dies nicht der Fall, so wird die Fehlermeldung gesetzt, die Informationen über das Auftreten der Assertion (Datei und Zeile) gesammelt und dann die Callback Funktion aufgerufen, die eine AssertException wirft. Am Ende der Funktion darf dabei nicht vergessen werden, die Fehlermeldung auf Null zu setzen, da es sonst bei Verwendung der normalen assert() Funktion passiert, dass die letzte Meldung ausgegeben wird.
Die fertige Klasse sollte dann wie folgt aussehen:
Code:
/**
* @package core::debugging
* @class Assert
*
* The Assert class reimplements the assert() function
* which is mostly used to debug sourcecode. It offers
* different methods to create formatted output if a
* given term is not correct. The last parameter of most
* methods is a string that holds the message to be displayed
* on failture.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 24.04.2010<br />
* Version 0.2, 21.05.2010 (Added methods to check for instance type)<br />
* Version 0.3, 21.05.2010 (Updated to singleton)<br />
*/
class Assert {
/**
* @private
* The current assertion message.
*/
private $msg;
/**
* @private
* The current class instance.
*/
private static $instance = null;
/**
* @public
*
* Returns an instance of the Assert class.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 21.05.2010<br />
*/
public static function getInstance() {
// Check if an instance already exists ...
if( self::$instance === null ) {
// ... and create one if not.
self::$instance = new self;
}
// Return the instance.
return self::$instance;
}
/**
* @private
*
* The default constructor uses the
* assert_options() function to setup
* the assertion configuration and overwrites
* the assert callback function.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 24.04.2010<br />
* Version 0.2, 21.05.2010 (Updated to singleton)<br />
*/
private function __construct() {
// Active assert and make it quiet.
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1);
// Finally set up the callback function.
assert_options(ASSERT_CALLBACK, array(&$this, 'callback'));
}
/**
* @private
*
* Avoid the cloning of this class.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 21.05.2010 <br />
*/
private function __clone() {}
/**
* @public
*
* Checks if two given values are equal and
* calls the callback function if not.
*
* @param mixed $actual The actual value.
* @param mixed $expected The expected value.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 24.04.2010<br />
*/
public function areEqual($actual, $expected, $msg = null) {
// Check if the given objects are equal ...
if($actual != $expected) {
// ... and create the assertion message if not.
$message = ($msg != null) ? $msg : 'Values are not equal';
// Then save the message, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback($info['file'], $info['line'], $actual . " == " . $expected);
}
}
/**
* @public
*
* Checks if two given values are not equal and
* calls the callback function if they are equal.
*
* @param mixed $actual The actual value.
* @param mixed $expected The expected value.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 24.04.2010<br />
*/
public function areNotEqual($actual, $expected, $msg = null) {
// Check if the given objects are not equal ...
if($actual == $expected) {
// ... and create the assertion message if not.
$message = ($msg != null) ? $msg : 'Values are equal';
// Then save the message, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback($info['file'], $info['line'], $actual . " != " . $expected);
}
}
/**
* @public
*
* Checks if two given arrays are equal and
* calls the callback function if they are not.
*
* @param array $actual The actual array.
* @param array $expected The expected array.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 20.05.2010<br />
*/
public function arrayEquals($actual, $expected, $msg = null) {
// Check if two arrays are equal ...
if( array_diff($actual, $expected) != null ) {
// ... and create the assertion message if not.
$message = ($msg != null) ? $msg : 'Arrays are not equal';
// Then save the message, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback(
$info['file'],
$info['line'],
print_r($actual, true) . " == " . print_r($expected, true)
);
}
}
/**
* @public
*
* Checks if two given arrays are not equal and
* calls the callback function if they are.
*
* @param array $actual The actual array.
* @param array $expected The expected array.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 20.05.2010<br />
*/
public function arrayNotEquals($actual, $expected, $msg = null) {
// Check if two arrays are equal ...
if( array_diff($actual, $expected) == null ) {
// ... and create the assertion message if they does.
$message = ($msg != null) ? $msg : 'Arrays are equal';
// Then save the message, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback(
$info['file'],
$info['line'],
print_r($actual, true) . " != " . print_r($expected, true)
);
}
}
/**
* @public
*
* Dumps a given variable in readable format.
*
* @param mixed $value The actual value.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 24.04.2010<br />
*/
public function dumpFormatted($value, $msg = null) {
// Create the message of this assertion, ...
$message = ($msg != null) ? $msg : 'Var Dump';
// ... save it, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and create the formatted dump.
$term = '<br /><pre>' . print_r($value, true) . '</pre>';
$this->callback($info['file'], $info['line'], $term);
}
/**
* @public
*
* Calls the callback function after saving the
* error message.
*
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 24.04.2010<br />
*/
public function fail($msg = null) {
// Set the actual error message, ...
$this->msg = $msg;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and call the callback function.
$this->callback($info['file'], $info['line'], "-> Just a break point.");
}
/**
* @public
*
* Gets the type of a given variable and dumps it.
*
* @param mixed $obj The variable.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 20.05.2010<br />
*/
public function getType($obj, $msg = null) {
// Check if it is an object ...
if( is_object($obj) === true ) {
$type = 'object ' . get_class($obj);
}
// ... a standard type ...
elseif( gettype($obj) != "unkown type" ) {
$type = gettype($obj);
}
// ... or unkown.
else {
$type = "unknown type";
}
// Then create the message of this assertion, ...
$message = ($msg != null) ? $msg : 'Type Dump';
// ... save it, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and create the formatted dump.
$term = $type;
$this->callback($info['file'], $info['line'], $term);
}
/**
* @public
*
* Checks if a given value is empty and
* calls the callback function if it is.
*
* @param mixed $value The current value.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 24.04.2010<br />
*/
public function isEmpty($value, $msg = null) {
// Check if the given object is empty ...
if( !empty($value) ) {
// ... and create the assertion message if not.
$message = ($msg != null) ? $msg : 'Value is not empty';
// Then save the message, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback($info['file'], $info['line'], $value);
}
}
/**
* @public
*
* Checks if a given term equals false and
* calls the callback function if it does not.
*
* @param mixed $term The term to be evaluated.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 24.04.2010<br />
*/
public function isFalse($term, $msg = null) {
// Check if the given term is false ...
if( eval("return " . $term . ";") !== false ) {
// ... and create the assertion message if not.
$message = ($msg != null) ? $msg : 'Term is not false';
// Then save the message, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback($info['file'], $info['line'], $term);
}
}
/**
* @public
*
* Checks if a given object is an instance of the specified
* class and calls the callback function if it is not.
*
* @param object $value The object.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 21.05.2010<br />
*/
public function isInstanceOf($obj, $type, $msg = null) {
// Check if the given value is an object ...
if( is_object($obj) ) {
// ... and of the requested type.
if( ($obj instanceof $type) == false ) {
// ... and create the assertion message if not.
$message = ($msg != null) ? $msg : 'Object is no instance of ' . $type;
// Then save the message, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback($info['file'], $info['line'], print_r($obj, true));
}
}
else {
// Save the message, ...
$this->msg = "Value is no instance!";
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback($info['file'], $info['line'], print_r($obj, true));
}
}
/**
* @public
*
* Checks if a given value is empty and
* calls the callback function if it is not.
*
* @param mixed $value The current value.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 24.04.2010<br />
*/
public function isNotEmpty($value, $msg = null) {
// Check if the given object is empty ...
if( empty($value) ) {
// ... and create the assertion message if not.
$message = ($msg != null) ? $msg : 'Value is empty';
// Then save the message, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback($info['file'], $info['line'], 'null');
}
}
/**
* @public
*
* Checks if a given object is an instance of the specified
* class and calls the callback function if it is.
*
* @param object $value The object.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 21.05.2010<br />
*/
public function isNotInstanceOf($obj, $type, $msg = null) {
// Check if the given value is an object ...
if( is_object($obj) ) {
// ... and of the requested type.
if( $obj instanceof $type ) {
// ... and create the assertion message if not.
$message = ($msg != null) ? $msg : 'Object is instance of ' . $type;
// Then save the message, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback($info['file'], $info['line'], print_r($obj, true));
}
}
else {
// Save the message, ...
$this->msg = "Value is no instance!";
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback($info['file'], $info['line'], print_r($obj, true));
}
}
/**
* @public
*
* Checks if a given value equals null and
* calls the callback function if it does.
*
* @param mixed $value The current value.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 20.05.2010<br />
*/
public function isNotNull($value, $msg = null) {
// Check if the given value equals null ...
if( $value === null ) {
// ... and create the assertion message if not.
$message = ($msg != null) ? $msg : 'Value equals null';
// Then save the message, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback($info['file'], $info['line'], 'null');
}
}
/**
* @public
*
* Checks if a given value equals null and
* calls the callback function if it does not.
*
* @param mixed $value The current value.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 20.05.2010<br />
*/
public function isNull($value, $msg = null) {
// Check if the given value equals null ...
if( $value !== null ) {
// ... and create the assertion message if not.
$message = ($msg != null) ? $msg : 'Value is not null';
// Then save the message, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback($info['file'], $info['line'], $value);
}
}
/**
* @public
*
* Checks if a given term equals true and
* calls the callback function if it does not.
*
* @param mixed $term The term to be evaluated.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 24.04.2010<br />
*/
public function isTrue($term, $msg = null) {
// Check if the given term is true ...
if( eval("return " . $term . ";") !== true ) {
// ... and create the assertion message if not.
$message = ($msg != null) ? $msg : 'Term is not true';
// Then save the message, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback($info['file'], $info['line'], $term);
}
}
/**
* @public
*
* Creates the string representation
* of a variable and dumps it.
*
* @param mixed $value The value or object.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 20.05.2010<br />
*/
public function toString($value, $msg = null) {
// Get the string representation of an object, ...
if( is_object($value) ) {
$string = print_r($value, true);
}
// ... an array ...
elseif( is_array($value) ) {
$string = print_r($value, true);
}
// ... an null type ...
elseif( $value == null || empty($value) ) {
$string = 'null';
}
// ... any other type.
else {
$string = sprintf($value);
}
// Create the message of this assertion, ...
$message = ($msg != null) ? $msg : 'The String Representation';
// ... save it, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and call the callback method.
$this->callback($info['file'], $info['line'], $string);
}
/**
* @public
*
* The callback function assigned by assert_options()
* to the assert function.
*
* @param string $file The calling file.
* @param integer $line The line number.
* @param string $code The checked code.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 24.04.2010<br />
*/
public function callback($file, $line, $code = null) {
// Get the error message, ...
$message =
($this->msg != null)
? '<b>' . $this->msg . '</b>'
: '<b>Assertion failed!</b>';
// ... reset the error message ...
$this->msg = null;
// ... and throw the assertion exception.
throw new AssertException(
$message
. ' in <i>' . $file . '</i>'
. ' line <i>' . $line . '</i><br />'
. ' <i>' . $code . '</i><br />'
);
}
}
Bevor die Klasse in unseren Projekten verwenden, sollten wir sie zuerst testen. Das ist in diesem Fall relativ einfach, da man viele Tests zufällig generieren lassen kann.
Zuerst erzeugt man eine Instanz der Assert-Klasse. Danach generiert man einfach ein paar zufällige Werte in einer Schleife und ruft die Funktion auf, die man testen möchte. Anhand der Ausgabe lässt sich relativ einfach feststellen, ob die Assertion korrekt erzeugt wurde. Dabei darf man allerdings nicht vergessen, den Code in der Schleife in einem try-catch-Block zu verpacken, da die Ausführung des Programms sonst direkt nach der ersten Exception abgebrochen wird. Außerdem würde es ohne eine solche Testumgebung einen Fehler aufgrund einer nicht gefangenen Exception geben.
Ein kurzer Test könnte also wie folgt aussehen:
Code:
<?php
require_once('Assert.php');
try {
// Create an instance of the assertion class.
$dbg = Assert::getInstance();
// Create 10 test cases.
for($i = 0; $i < 10; $i++) {
// Create some random numbers ...
$one = 0;
$two = rand(0, 1);
// ... and check them.
try {
$dbg->areEqual($one, $two);
} catch(AssertException $e) {
print $e->getMessage();
}
}
// Create 10 more test cases.
for($i = 0; $i < 10; $i++) {
// Create some random numbers ...
$one = rand(0, 1);
$two = rand(0, 1);
// ... and check them.
try {
$dbg->areNotEqual($one, $two, 'The given values are equal!');
} catch(AssertException $e) {
print $e->getMessage();
}
}
// Create 10 more test cases.
for($i = 0; $i < 10; $i++) {
// Create some random numbers ...
$term = rand(0, 1) . "+" . rand(0, 1) . "==" . rand(0, 1);
// ... and check them.
try {
$dbg->isFalse($term);
} catch(AssertException $e) {
print $e->getMessage();
}
}
// Create 10 more test cases.
for($i = 0; $i < 10; $i++) {
// Create some random numbers ...
$term = rand(0, 1) . "+" . rand(0, 1) . "==" . rand(0, 1);
// ... and check them.
try {
$dbg->isTrue($term, 'The term does not match true!');
} catch(AssertException $e) {
print $e->getMessage();
}
}
try {
$var = array("a" => "Mein tolles Ding!", "b" => "Noch son Ding!");
$dbg->getType($var);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->getType($dbg);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$var = array("a" => "Mein tolles Ding!", "b" => "Noch son Ding!");
$dbg->dumpFormatted($var);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->dumpFormatted($dbg);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$var2 = array("a" => "Dein tolles Teil!", "b" => "Noch son Teil!");
$dbg->arrayEquals($var, $var2);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->arrayNotEquals($var, $var);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->isEmpty($var);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->isNotEmpty(null);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->isNull('blubb');
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->isNotNull(null);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->toString($dbg);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->toString($var);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->toString(666);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->toString("sdfsfd");
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->toString(null);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->toString(array());
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->isInstanceOf("", 'stdClass');
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->isInstanceOf($dbg, 'stdClass');
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->isInstanceOf($dbg, 'Assert');
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->fail();
} catch(AssertException $e) {
print $e->getMessage();
}
try {
assert(false && 'The normal assert function!');
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->fail('End of assertion test.');
} catch(AssertException $e) {
print $e->getMessage();
}
}
catch(Exception $e) {
print $e->getMessage();
}
?>
Fragen und Antworten findet ihr selbstverständlich bei uns im Forum. Ich möchte euch auch darum bitten, keine Fragen an mich persönlich zu senden aus dem einfachen Grund, dass andere Menschen dann nichts von den gegebenen Antworten haben. Zudem können euch im Forum auch viele andere Leute, die echt Ahnung haben, helfen. Natürlich freue ich mich über ein kurzes Feedback zu diesem Tutorial. Ich bedanke mich für eure Aufmerksamkeit und hoffe, dass euch mein kurzer Exkurs gefallen hat.
Liebe Grüße,
Andreas
http://www.avedo.net
>> Allgemeine Fragen oder Probleme mit dem Tutorial? Hier gehts zum Forum!
Assertions in PHP
Wie viele andere Programmiersprachen bietet auch PHP seinem Anwender die Möglichkeit, seine Programme mit Hilfe von Assertions zu debuggen. Die assert()-Funktion überprüft dabei einen übergebenen Term auf seine Korrektheit und gibt eine Nachricht aus, falls dieser nicht erfüllt ist. Es ist dabei auch möglich den Term als String zu übergeben. Dieser wird dann als PHP-Code ausgeführt. Die Verwendung einer String-Assertion hat den Vorteil, dass sie weniger Arbeit machen, falls Assertions deaktiviert sind, und außerdem ist es so möglich den Assertion-Code auszugeben, falls dieser mit false evaluiert wird. Das bedeutet, dass ein Boolescher Ausdruck, der an die assert() Funktion übergeben wird, nicht als weiterer Parameter an die via assert_options() definierte Funktion übergeben wird. Es wird also nur ein leerer String anstatt des Terms ausgegeben.
Wie sich bereits vermuten lässt, falls es nicht so oder so schon bekannt ist, ist die Verwendung von Assertions nur zum Debuggen von Quellcode und nicht zur Überprüfung von Parametern in einem tatsächlichen Programm gedacht.
Die assert() Funktion beeinflussen
PHP bietet einem die Möglichkeit die Funktionsweise der assert() Funktion zu beeinflussen. Dies ist zum einen über entsprechende Einstellungen in den ini-Einstellungen möglich, aber auch, wie ich bereits erwähnte, über die Funktion assert_options(). In beiden Fällen ist es möglich eine Callback Funktion zu definieren, die im Falle einer fehlgeschlagenen Assertion aufgerufen wird.
Diese Callback-Funktion sollte drei Parameter enthalten: die Datei und die Zeile in der die Assertion auftrat sowie optional den Code, der diese verursachte. Diese Informationen kann man relativ einfach via debug_backtrace() abfragen.
Entwurf einer Assertion Klasse
Wir wollen nun die Möglichkeit von PHP nutzen, die Funktionsweise von Assertions zu beeinflussen, um eine eigene Assertion-Klasse zu implementieren. Dazu sollten wir uns jedoch als erstes fragen, welche Anforderungen wir an eine solche Klasse stellen wollen.
Die Klasse soll natürlich eine eigene Callback-Methode definieren, die es uns ermöglicht, die Ausgabe unserer Assertions zu beeinflussen. Außerdem soll sie verschiedene Methoden bereitstellen, die entweder übergebene Variablen oder Terme auf ihre Korrektheit prüfen oder diese schlichtweg ausgeben. Des weiteren wäre es schön, wenn es Methoden gäbe, die den Typ einer Variablen ausgeben oder auf einen bestimmten Typ prüfen. Zudem wäre es schön, wenn es möglich wäre optional eigene Fehlermeldungen zu definieren. Zuletzt sollte es noch eine Methode geben, die es ihrem Anwender ermöglicht, einfach nur an einer bestimmten Stelle die Ausführung des Quellcodes zu stoppen und dabei eine Ausgabe zu machen. Es sollen daher die nachfolgenden Methoden implementiert werden.
* areEqual($actual, $expected, $msg = null)
* areNotEqual($actual, $expected, $msg = null)
* arrayEquals($actual, $expected, $msg = null)
* arrayNotEquals($actual, $expected, $msg = null)
* dumpFormatted($value, $msg = null)
* fail($msg = null)
* getType($obj, $msg = null)
* isEmpty($value, $msg = null)
* isFalse($term, $msg = null)
* isInstanceOf($obj, $type, $msg = null)
* isNotEmpty($value, $msg = null)
* isNotInstanceOf($obj, $type, $msg = null)
* isNotNull($value, $msg = null)
* isNull($value, $msg = null)
* isTrue($term, $msg = null)
* toString($value, $msg = null)
* callback($file, $line, $code = null)
Bevor wir jedoch mit der Implementierung dieser Methoden beginnen, sollten wir uns noch über einige Dinge im Klaren werden. Es soll weiterhin möglich sein die assert() Funktion ganz normal zu verwenden. Der Programmablauf soll beim Auftreten einer Assertion nicht unbedingt unterbrochen werden. Die Verwendung von die() oder exit() fallen also aus. Es ist so oder so viel schöner, wenn man Exceptions verwendet. Um diese jedoch getrennt von anderen Ausnahmen behandeln zu können, sollte man eine eigene Ausnahme definieren. Wir werden diese AssertException nennen. Ihre Implementierung sieht wie folgt aus.
Code:
/**
* @package core::debugging
* @class AssertException
*
* The AssertException class implements a
* Exception type for the Assert class.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 24.04.2010<br />
*/
class AssertException extends Exception {}
Zuletzt sollte uns auch klar sein, dass es wenig Sinn macht beliebig viele Instanzen der Assertion Klasse zu erzeugen. Es ist daher sinnvoll, diese als Singleton zu entwerfen.
Nun kann die eigentliche Programmierarbeit beginnen. Die einzelnen Assertion-Methoden haben dabei alle die gleiche Struktur. Als erstes wird geprüft, ob die Bedingung, nach der gefragt wird, erfüllt ist. Ist dies nicht der Fall, so wird die Fehlermeldung gesetzt, die Informationen über das Auftreten der Assertion (Datei und Zeile) gesammelt und dann die Callback Funktion aufgerufen, die eine AssertException wirft. Am Ende der Funktion darf dabei nicht vergessen werden, die Fehlermeldung auf Null zu setzen, da es sonst bei Verwendung der normalen assert() Funktion passiert, dass die letzte Meldung ausgegeben wird.
Die fertige Klasse sollte dann wie folgt aussehen:
Code:
/**
* @package core::debugging
* @class Assert
*
* The Assert class reimplements the assert() function
* which is mostly used to debug sourcecode. It offers
* different methods to create formatted output if a
* given term is not correct. The last parameter of most
* methods is a string that holds the message to be displayed
* on failture.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 24.04.2010<br />
* Version 0.2, 21.05.2010 (Added methods to check for instance type)<br />
* Version 0.3, 21.05.2010 (Updated to singleton)<br />
*/
class Assert {
/**
* @private
* The current assertion message.
*/
private $msg;
/**
* @private
* The current class instance.
*/
private static $instance = null;
/**
* @public
*
* Returns an instance of the Assert class.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 21.05.2010<br />
*/
public static function getInstance() {
// Check if an instance already exists ...
if( self::$instance === null ) {
// ... and create one if not.
self::$instance = new self;
}
// Return the instance.
return self::$instance;
}
/**
* @private
*
* The default constructor uses the
* assert_options() function to setup
* the assertion configuration and overwrites
* the assert callback function.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 24.04.2010<br />
* Version 0.2, 21.05.2010 (Updated to singleton)<br />
*/
private function __construct() {
// Active assert and make it quiet.
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1);
// Finally set up the callback function.
assert_options(ASSERT_CALLBACK, array(&$this, 'callback'));
}
/**
* @private
*
* Avoid the cloning of this class.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 21.05.2010 <br />
*/
private function __clone() {}
/**
* @public
*
* Checks if two given values are equal and
* calls the callback function if not.
*
* @param mixed $actual The actual value.
* @param mixed $expected The expected value.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 24.04.2010<br />
*/
public function areEqual($actual, $expected, $msg = null) {
// Check if the given objects are equal ...
if($actual != $expected) {
// ... and create the assertion message if not.
$message = ($msg != null) ? $msg : 'Values are not equal';
// Then save the message, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback($info['file'], $info['line'], $actual . " == " . $expected);
}
}
/**
* @public
*
* Checks if two given values are not equal and
* calls the callback function if they are equal.
*
* @param mixed $actual The actual value.
* @param mixed $expected The expected value.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 24.04.2010<br />
*/
public function areNotEqual($actual, $expected, $msg = null) {
// Check if the given objects are not equal ...
if($actual == $expected) {
// ... and create the assertion message if not.
$message = ($msg != null) ? $msg : 'Values are equal';
// Then save the message, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback($info['file'], $info['line'], $actual . " != " . $expected);
}
}
/**
* @public
*
* Checks if two given arrays are equal and
* calls the callback function if they are not.
*
* @param array $actual The actual array.
* @param array $expected The expected array.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 20.05.2010<br />
*/
public function arrayEquals($actual, $expected, $msg = null) {
// Check if two arrays are equal ...
if( array_diff($actual, $expected) != null ) {
// ... and create the assertion message if not.
$message = ($msg != null) ? $msg : 'Arrays are not equal';
// Then save the message, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback(
$info['file'],
$info['line'],
print_r($actual, true) . " == " . print_r($expected, true)
);
}
}
/**
* @public
*
* Checks if two given arrays are not equal and
* calls the callback function if they are.
*
* @param array $actual The actual array.
* @param array $expected The expected array.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 20.05.2010<br />
*/
public function arrayNotEquals($actual, $expected, $msg = null) {
// Check if two arrays are equal ...
if( array_diff($actual, $expected) == null ) {
// ... and create the assertion message if they does.
$message = ($msg != null) ? $msg : 'Arrays are equal';
// Then save the message, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback(
$info['file'],
$info['line'],
print_r($actual, true) . " != " . print_r($expected, true)
);
}
}
/**
* @public
*
* Dumps a given variable in readable format.
*
* @param mixed $value The actual value.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 24.04.2010<br />
*/
public function dumpFormatted($value, $msg = null) {
// Create the message of this assertion, ...
$message = ($msg != null) ? $msg : 'Var Dump';
// ... save it, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and create the formatted dump.
$term = '<br /><pre>' . print_r($value, true) . '</pre>';
$this->callback($info['file'], $info['line'], $term);
}
/**
* @public
*
* Calls the callback function after saving the
* error message.
*
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 24.04.2010<br />
*/
public function fail($msg = null) {
// Set the actual error message, ...
$this->msg = $msg;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and call the callback function.
$this->callback($info['file'], $info['line'], "-> Just a break point.");
}
/**
* @public
*
* Gets the type of a given variable and dumps it.
*
* @param mixed $obj The variable.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 20.05.2010<br />
*/
public function getType($obj, $msg = null) {
// Check if it is an object ...
if( is_object($obj) === true ) {
$type = 'object ' . get_class($obj);
}
// ... a standard type ...
elseif( gettype($obj) != "unkown type" ) {
$type = gettype($obj);
}
// ... or unkown.
else {
$type = "unknown type";
}
// Then create the message of this assertion, ...
$message = ($msg != null) ? $msg : 'Type Dump';
// ... save it, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and create the formatted dump.
$term = $type;
$this->callback($info['file'], $info['line'], $term);
}
/**
* @public
*
* Checks if a given value is empty and
* calls the callback function if it is.
*
* @param mixed $value The current value.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 24.04.2010<br />
*/
public function isEmpty($value, $msg = null) {
// Check if the given object is empty ...
if( !empty($value) ) {
// ... and create the assertion message if not.
$message = ($msg != null) ? $msg : 'Value is not empty';
// Then save the message, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback($info['file'], $info['line'], $value);
}
}
/**
* @public
*
* Checks if a given term equals false and
* calls the callback function if it does not.
*
* @param mixed $term The term to be evaluated.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 24.04.2010<br />
*/
public function isFalse($term, $msg = null) {
// Check if the given term is false ...
if( eval("return " . $term . ";") !== false ) {
// ... and create the assertion message if not.
$message = ($msg != null) ? $msg : 'Term is not false';
// Then save the message, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback($info['file'], $info['line'], $term);
}
}
/**
* @public
*
* Checks if a given object is an instance of the specified
* class and calls the callback function if it is not.
*
* @param object $value The object.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 21.05.2010<br />
*/
public function isInstanceOf($obj, $type, $msg = null) {
// Check if the given value is an object ...
if( is_object($obj) ) {
// ... and of the requested type.
if( ($obj instanceof $type) == false ) {
// ... and create the assertion message if not.
$message = ($msg != null) ? $msg : 'Object is no instance of ' . $type;
// Then save the message, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback($info['file'], $info['line'], print_r($obj, true));
}
}
else {
// Save the message, ...
$this->msg = "Value is no instance!";
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback($info['file'], $info['line'], print_r($obj, true));
}
}
/**
* @public
*
* Checks if a given value is empty and
* calls the callback function if it is not.
*
* @param mixed $value The current value.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 24.04.2010<br />
*/
public function isNotEmpty($value, $msg = null) {
// Check if the given object is empty ...
if( empty($value) ) {
// ... and create the assertion message if not.
$message = ($msg != null) ? $msg : 'Value is empty';
// Then save the message, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback($info['file'], $info['line'], 'null');
}
}
/**
* @public
*
* Checks if a given object is an instance of the specified
* class and calls the callback function if it is.
*
* @param object $value The object.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 21.05.2010<br />
*/
public function isNotInstanceOf($obj, $type, $msg = null) {
// Check if the given value is an object ...
if( is_object($obj) ) {
// ... and of the requested type.
if( $obj instanceof $type ) {
// ... and create the assertion message if not.
$message = ($msg != null) ? $msg : 'Object is instance of ' . $type;
// Then save the message, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback($info['file'], $info['line'], print_r($obj, true));
}
}
else {
// Save the message, ...
$this->msg = "Value is no instance!";
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback($info['file'], $info['line'], print_r($obj, true));
}
}
/**
* @public
*
* Checks if a given value equals null and
* calls the callback function if it does.
*
* @param mixed $value The current value.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 20.05.2010<br />
*/
public function isNotNull($value, $msg = null) {
// Check if the given value equals null ...
if( $value === null ) {
// ... and create the assertion message if not.
$message = ($msg != null) ? $msg : 'Value equals null';
// Then save the message, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback($info['file'], $info['line'], 'null');
}
}
/**
* @public
*
* Checks if a given value equals null and
* calls the callback function if it does not.
*
* @param mixed $value The current value.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 20.05.2010<br />
*/
public function isNull($value, $msg = null) {
// Check if the given value equals null ...
if( $value !== null ) {
// ... and create the assertion message if not.
$message = ($msg != null) ? $msg : 'Value is not null';
// Then save the message, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback($info['file'], $info['line'], $value);
}
}
/**
* @public
*
* Checks if a given term equals true and
* calls the callback function if it does not.
*
* @param mixed $term The term to be evaluated.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 24.04.2010<br />
*/
public function isTrue($term, $msg = null) {
// Check if the given term is true ...
if( eval("return " . $term . ";") !== true ) {
// ... and create the assertion message if not.
$message = ($msg != null) ? $msg : 'Term is not true';
// Then save the message, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and finally call the callback method.
$this->callback($info['file'], $info['line'], $term);
}
}
/**
* @public
*
* Creates the string representation
* of a variable and dumps it.
*
* @param mixed $value The value or object.
* @param string $msg The error message.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 20.05.2010<br />
*/
public function toString($value, $msg = null) {
// Get the string representation of an object, ...
if( is_object($value) ) {
$string = print_r($value, true);
}
// ... an array ...
elseif( is_array($value) ) {
$string = print_r($value, true);
}
// ... an null type ...
elseif( $value == null || empty($value) ) {
$string = 'null';
}
// ... any other type.
else {
$string = sprintf($value);
}
// Create the message of this assertion, ...
$message = ($msg != null) ? $msg : 'The String Representation';
// ... save it, ...
$this->msg = $message;
// ... get all information of the calling script ...
$info = debug_backtrace();
$info = $info[0];
// ... and call the callback method.
$this->callback($info['file'], $info['line'], $string);
}
/**
* @public
*
* The callback function assigned by assert_options()
* to the assert function.
*
* @param string $file The calling file.
* @param integer $line The line number.
* @param string $code The checked code.
*
* @author Andreas Wilhelm
* @version
* Version 0.1, 24.04.2010<br />
*/
public function callback($file, $line, $code = null) {
// Get the error message, ...
$message =
($this->msg != null)
? '<b>' . $this->msg . '</b>'
: '<b>Assertion failed!</b>';
// ... reset the error message ...
$this->msg = null;
// ... and throw the assertion exception.
throw new AssertException(
$message
. ' in <i>' . $file . '</i>'
. ' line <i>' . $line . '</i><br />'
. ' <i>' . $code . '</i><br />'
);
}
}
Test und Anwendungsbeispiel
Bevor die Klasse in unseren Projekten verwenden, sollten wir sie zuerst testen. Das ist in diesem Fall relativ einfach, da man viele Tests zufällig generieren lassen kann.
Zuerst erzeugt man eine Instanz der Assert-Klasse. Danach generiert man einfach ein paar zufällige Werte in einer Schleife und ruft die Funktion auf, die man testen möchte. Anhand der Ausgabe lässt sich relativ einfach feststellen, ob die Assertion korrekt erzeugt wurde. Dabei darf man allerdings nicht vergessen, den Code in der Schleife in einem try-catch-Block zu verpacken, da die Ausführung des Programms sonst direkt nach der ersten Exception abgebrochen wird. Außerdem würde es ohne eine solche Testumgebung einen Fehler aufgrund einer nicht gefangenen Exception geben.
Ein kurzer Test könnte also wie folgt aussehen:
Code:
<?php
require_once('Assert.php');
try {
// Create an instance of the assertion class.
$dbg = Assert::getInstance();
// Create 10 test cases.
for($i = 0; $i < 10; $i++) {
// Create some random numbers ...
$one = 0;
$two = rand(0, 1);
// ... and check them.
try {
$dbg->areEqual($one, $two);
} catch(AssertException $e) {
print $e->getMessage();
}
}
// Create 10 more test cases.
for($i = 0; $i < 10; $i++) {
// Create some random numbers ...
$one = rand(0, 1);
$two = rand(0, 1);
// ... and check them.
try {
$dbg->areNotEqual($one, $two, 'The given values are equal!');
} catch(AssertException $e) {
print $e->getMessage();
}
}
// Create 10 more test cases.
for($i = 0; $i < 10; $i++) {
// Create some random numbers ...
$term = rand(0, 1) . "+" . rand(0, 1) . "==" . rand(0, 1);
// ... and check them.
try {
$dbg->isFalse($term);
} catch(AssertException $e) {
print $e->getMessage();
}
}
// Create 10 more test cases.
for($i = 0; $i < 10; $i++) {
// Create some random numbers ...
$term = rand(0, 1) . "+" . rand(0, 1) . "==" . rand(0, 1);
// ... and check them.
try {
$dbg->isTrue($term, 'The term does not match true!');
} catch(AssertException $e) {
print $e->getMessage();
}
}
try {
$var = array("a" => "Mein tolles Ding!", "b" => "Noch son Ding!");
$dbg->getType($var);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->getType($dbg);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$var = array("a" => "Mein tolles Ding!", "b" => "Noch son Ding!");
$dbg->dumpFormatted($var);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->dumpFormatted($dbg);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$var2 = array("a" => "Dein tolles Teil!", "b" => "Noch son Teil!");
$dbg->arrayEquals($var, $var2);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->arrayNotEquals($var, $var);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->isEmpty($var);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->isNotEmpty(null);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->isNull('blubb');
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->isNotNull(null);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->toString($dbg);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->toString($var);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->toString(666);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->toString("sdfsfd");
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->toString(null);
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->toString(array());
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->isInstanceOf("", 'stdClass');
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->isInstanceOf($dbg, 'stdClass');
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->isInstanceOf($dbg, 'Assert');
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->fail();
} catch(AssertException $e) {
print $e->getMessage();
}
try {
assert(false && 'The normal assert function!');
} catch(AssertException $e) {
print $e->getMessage();
}
try {
$dbg->fail('End of assertion test.');
} catch(AssertException $e) {
print $e->getMessage();
}
}
catch(Exception $e) {
print $e->getMessage();
}
?>
Fragen und Antworten
Fragen und Antworten findet ihr selbstverständlich bei uns im Forum. Ich möchte euch auch darum bitten, keine Fragen an mich persönlich zu senden aus dem einfachen Grund, dass andere Menschen dann nichts von den gegebenen Antworten haben. Zudem können euch im Forum auch viele andere Leute, die echt Ahnung haben, helfen. Natürlich freue ich mich über ein kurzes Feedback zu diesem Tutorial. Ich bedanke mich für eure Aufmerksamkeit und hoffe, dass euch mein kurzer Exkurs gefallen hat.
Liebe Grüße,
Andreas
http://www.avedo.net
>> Allgemeine Fragen oder Probleme mit dem Tutorial? Hier gehts zum Forum!