Req #70604 [Opn->Csd]: Magic Integer casting + NULL cast

From: Date: Sat, 06 Aug 2016 02:31:10 +0000
Subject: Req #70604 [Opn->Csd]: Magic Integer casting + NULL cast
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-202982@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70604&edit=1 ID: 70604 Updated by: kalle@php.net Reported by: jodminster at gmx dot net Summary: Magic Integer casting + NULL cast -Status: Open +Status: Closed Type: Feature/Change Request Package: *General Issues PHP Version: Irrelevant -Assigned To: +Assigned To: kalle Block user comment: N Private report: N New Comment: 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! Previous Comments: ------------------------------------------------------------------------ [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

« previous php.bugs (#202982) next »