note 50239 deleted from langref by betz

From: Date: Fri, 29 Jul 2005 09:44:12 +0000
Subject: note 50239 deleted from langref by betz
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-92713@lists.php.net to get a copy of this message
Note Submitter: michael dot bommarito at gmail dot com ---- Posted this on the function-handling function page, but it seems it's a topic here too. I've developed an easy-to-use hack using these functions to emulate overloaded functions. It's especially handy for constructors and/or C++ style templating. Here's a little example to get you going. This does the trick for most circumstances, and if you need more intelligent parsing, it's not too hard to implement with regex or a more suitable classification scheme. N.B. Note the lack of whitespace between variable types in case strings. class Matrix { ... function Matrix() { $args = func_get_args(); $argmatch = implode(",", array_map('gettype', $args)); switch( $argmatch ) { case 'integer,integer': //initialize m x n matrix break; case 'integer,integer,integer': //initialize m x n matrix with constant c break; case 'integer,integer,float': //initialize m x n matrix with constant c break; case 'array': //initialize from array (2D....) break; case 'array,integer': //intialize from array (1D packed with m rows) break; default: //(void) overload? //error handling? break; } } ... }

« previous php.notes (#92713) next »