note 50239 added to langref
| From: | michael dot bommarito at gmail dot com | Date: | Tue, 22 Feb 2005 19:16:58 +0000 |
| Subject: | note 50239 added to langref | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85383@lists.php.net to get a copy of this message | ||
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;
}
}
...
}
----
Manual Page -- http://www.php.net/manual/en/langref.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50239
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50239&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50239&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50239&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50239&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50239&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50239&report=yes
Search -- http://master.php.net/manage/user-notes.php