rfc:class_casting_to_scalar

Request for Comments: Class casting to scalar

This RFC covers a way to cast classes to scalar values other than strings.

Introduction

Currently the Zend Engine provides some magic to methods in classes with defined names, for example __toString() is called when someone attempts to cast a class to a string.

The purpose of this RFC is to propose a way to allow casting to other scalar types (boolean, double, integer).

Why casting?

For the same reasons as __toString() exists, casting to other types might be needed. Feature requests have been submitted regarding this in the past.

Possible features/implementations

Interface or magic method?

Currently two possibilities exists:

  • Implementing a new magic method, for example __scalarValue() or __cast() in the Zend Engine.
  • Creating a new interface (for example ScalarAccess?) and force the presence of one function (scalarValue()?) which will return the scalar value for this class.

Pass requested type?

Concern has been raised over the fact of passing the requested value type as parameter to the function, as it could be used to disallow some kinds of cast.

Proposal and Patch

Rejected Features

  • None yet

Bugs

  • When used with math operators, it seems we *have* to cast to integer if not explictly requested in the code. TODO: find a way to force our way with a float value if needed

Changelog

2008-11-24: Initial draft

rfc/class_casting_to_scalar.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1