note 49512 added to ref.funchand
| From: | michael dot bommarito at gmail dot com | Date: | Sun, 30 Jan 2005 19:28:59 +0000 |
| Subject: | note 49512 added to ref.funchand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-84248@lists.php.net to get a copy of this message | ||
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/ref.funchand.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+49512
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49512&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49512&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49512&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49512&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49512&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+49512&report=yes
Search -- http://master.php.net/manage/user-notes.php