#50599 [NEW]: Magic method for overriding gettype() results.
| From: | jmcentire at gmail dot com | Date: | Tue, 29 Dec 2009 00:35:43 +0000 |
| Subject: | #50599 [NEW]: Magic method for overriding gettype() results. | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-146169@lists.php.net to get a copy of this message | ||
From: jmcentire at gmail dot com
Operating system: Debian
PHP version: 5.3.1
PHP Bug Type: Feature/Change Request
Bug description: Magic method for overriding gettype() results.
Description:
------------
I'm modeling my data as objects and would like it to be as transparent
as possible. As such, gettype($myObj) should return a type value other
than 'object.' Can you implement a magic method __gettype() which
expects the return value to be a string?
Reproduce code:
---------------
---
From manual page: language.oop5.magic
---
class MyInt {
public function __construct ($val){
$this->val = $val;
}
public function __gettype () {
return 'int';
}
}
$foo = new MyInt(5);
$int = (int) 5;
if (gettype($foo) == gettype($int)){
echo gettype($foo); // Should echo 'int'
} else {
echo "Fail."
}
Expected result:
----------------
Ideally, the code above would echo "int" as the type of MyInt $foo is
overridden by the __gettype magic method to be 'int', just as it is for
$int.
Actual result:
--------------
The code above will echo "Fail." You may also need to implement a
type-agnostic equivalent of __toString() like __return() that does,
essentially:
public function __return () {
return ($this->__gettype()) $this->_value;
}
This would allow me to compare:
($foo === $int); // This should evaluate to true.
--
Edit bug report at http://bugs.php.net/?id=50599&edit=1
--
Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=50599&r=trysnapshot52
Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=50599&r=trysnapshot53
Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=50599&r=trysnapshot60
Fixed in SVN: http://bugs.php.net/fix.php?id=50599&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=50599&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=50599&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=50599&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=50599&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=50599&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=50599&r=support
Expected behavior: http://bugs.php.net/fix.php?id=50599&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=50599&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=50599&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=50599&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=50599&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=50599&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=50599&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=50599&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=50599&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=50599&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=50599&r=mysqlcfg