Skip to Main Content
PHP 5 Kochbuch, Third Edition
book

PHP 5 Kochbuch, Third Edition

by David Sklar, Adam Trachtenberg, Carsten Lucke, Matthias Brusdeylins, Ulrich Speidel, Stephan Schmidt
September 2009
Intermediate to advanced content levelIntermediate to advanced
912 pages
48h 11m
German
O'Reilly Verlag
Content preview from PHP 5 Kochbuch, Third Edition

7.19 Methodenpolymorphie einsetzen

Problem

Sie möchten in Abhängigkeit von der Anzahl und dem Typ der an eine Methode übergebenen Argumente jeweils unterschiedlichen Code ausführen.

Lösung

Methodenpolymorphie unterstützt PHP nicht als eingebaute Funktionalität. Sie lässt sich aber mithilfe verschiedener Funktionen zur Typprüfung emulieren. Die folgende kombinieren( )-Methode nutzt is_numeric( ), is_string( ), is_array( ) und is_bool( ):

// kombinieren() addiert Zahlen, verkettet Strings, verschmilzt Arrays // und verknüpft Boolesche Argumente mit einem bitweisen UND. function kombinieren($a, $b) { if (is_int($a) && is_int($b)) { return $a + $b; } if (is_float($a) && is_float($b)) { return $a + $b; } if (is_string($a) && is_string($b)) { return "$a$b"; ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP programmieren unter Windows

PHP programmieren unter Windows

Arno Hollosi
CSS Kochbuch, 3rd Edition

CSS Kochbuch, 3rd Edition

Joergen Lang, Christopher Schmitt

Publisher Resources

ISBN: 9783868993271Purchase book