Exceptions for XPClass::get*()

  Status: implemented,  Wed Sep 3 19:28:34 2008
  • Created: 2008-04-26 12:44:23+0200
  • Categories: lang
  • Author: friebe

Scope of Change

The following methods in lang.XPClass will throw exceptions instead of returning NULL for the case the respective element is not found:

  • getConstructor()
  • getMethod(string name)
  • getField(string name)


Rationale

Allow for chaining, be consistent with other APIs (if a hasX method exists, the getX method will throw an exception) and the getAnnotation() method.

Functionality

The following can be written in a more concise way:

  $m= XPClass::forName('util.cmd.Runner')->getMethod('main');
if (!$m) throw new ElementNotFoundException('No "main" method');
$m->invoke(NULL, array($args));

New:

  XPClass::forName('util.cmd.Runner')->getMethod('main')->invoke(NULL, array($args));



BC break

The following will no longer work:

  if (!($m= $class->getMethod($name))) {
// The method does not exist
}

...and must be rewritten to use hasMethod():

  if (!$class->hasMethod($name)) {
// The method does not exist
}
$m= $class->getMethod($name);

If the first version is run, the block below the getMethod() call will never be reached (as it will throw an exception).

Security considerations

n/a

Speed impact

None.

Dependencies

- BC break - increases minor version (at the time of writing, that would mean the next release would be 5.7.0)

Related documents

- http://xp-framework.net/rfc/contrib/rfc0164.diff Implementing patch

Comments



<EOF>


Table of contents