LonghornPHP 2026

Voting

: one plus four?
(Example: nine)

The Note You're Voting On

michael dot bommarito at gmail dot com
21 years ago
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;
  }
}

...

}

<< Back to user notes page

To Top