Req #52583 [NEW]: Improved casting and hinting, new magic method __cast()
| From: | martin dot leucht at gmail dot com | Date: | Wed, 11 Aug 2010 14:30:03 +0000 |
| Subject: | Req #52583 [NEW]: Improved casting and hinting, new magic method __cast() | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-153326@lists.php.net to get a copy of this message | ||
From:
Operating system: Irrelevant
PHP version: Irrelevant
Package: Class/Object related
Bug Type: Feature/Change Request
Bug description:Improved casting and hinting, new magic method __cast()
Description:
------------
Type casting in PHP is currently limited to the builtin types. It would be
very useful (IMO) if one could cast a value/variable to a class. And also
type hinting could be improved by casting instead of type checking only.
I suggest to solve this by introducing a new magic method for classes,
called "__cast()", that accepts the value to cast as parameter.
I see two possible solutions for its functionality:
(1) __cast() replaces the constructor method
This will force the developer to move logics from the constructor into a
separate function/method (which isn't even so bad) or to copy his code
(which is indeed bad). If the function returns with the boolean value
"false", the default error mechanism is started saying something like "cast
to XYZ not allowed".
(2) __cast() acts like a static constructor
The code must return the casted result itself. That means one would
implement some logics to transform the given value to the needed
constructor parameters and create a new instance on his own. To handle an
unsupported value the method would throw an exception or an error. The
negative (or let's say curious) thing about this solution is, that this
cast method could also return a value of a totally different type (see
example).
Test script:
---------------
// see patch file
Expected result:
----------------
// working cast
Actual result:
--------------
// parse errors
--
Edit bug report at http://bugs.php.net/bug.php?id=52583&edit=1
--
Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=52583&r=trysnapshot52
Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=52583&r=trysnapshot53
Try a snapshot (trunk): http://bugs.php.net/fix.php?id=52583&r=trysnapshottrunk
Fixed in SVN: http://bugs.php.net/fix.php?id=52583&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=52583&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=52583&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=52583&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=52583&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=52583&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=52583&r=support
Expected behavior: http://bugs.php.net/fix.php?id=52583&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=52583&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=52583&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=52583&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=52583&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=52583&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=52583&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=52583&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=52583&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=52583&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=52583&r=mysqlcfg