Req #70604 [Csd->Sus]: Magic Integer casting + NULL cast
Edit report at https://bugs.php.net/bug.php?id=70604&edit=1
ID: 70604
Updated by: nikic@php.net
Reported by: jodminster at gmx dot net
Summary: Magic Integer casting + NULL cast
-Status: Closed
+Status: Suspended
Type: Feature/Change Request
Package: *General Issues
PHP Version: Irrelevant
Assigned To: kalle
Block user comment: N
Private report: N
Previous Comments:
------------------------------------------------------------------------
[2016-08-06 02:31:08] kalle@php.net
This issue have been brought up multiple times to allow overloading for casting objects to other
times besides string.
I suggest you to mail to internals@ and perhaps write an RFC about this, as it will bring more
attention to the feature.
Thanks for helping to make PHP even greater!
------------------------------------------------------------------------
[2015-09-30 08:47:25] jodminster at gmx dot net
Description:
------------
Analog to __toString() a __toInt() method.
Additionally allow NULL as a valid return value for casting methods.
Test script:
---------------
class test {
private $value;
function __construct($value = null) {
$this->value = $value;
}
function __toString() {
return $this->value ? (string) $this->value : $this->value;
}
function __toInt() {
return $this->value ? (int) $this->value : $this->value;
}
}
(string)(new test()) === null;
(string)(new test('1')) === '1';
(string)(new test(1)) === '1';
(int)(new test()) === null;
(int)(new test('1') === 1;
(int)(new test(1) === 1;
Expected result:
----------------
6 x true
Actual result:
--------------
* Catchable fatal error: Method test::__toString() must return a string value in php shell code on
line 1
*__toInt is never called
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70604&edit=1
Thread (3 messages)