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

From: Date: Sat, 06 Aug 2016 09:36:50 +0000
Subject: Req #70604 [Csd->Sus]: Magic Integer casting + NULL cast
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-202996@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:         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)

« previous php.bugs (#202996) next »