#50599 [Bgs]: Magic method for overriding gettype() results.

From: Date: Tue, 29 Dec 2009 17:16:21 +0000
Subject: #50599 [Bgs]: Magic method for overriding gettype() results.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-146195@lists.php.net to get a copy of this message
ID: 50599 User updated by: jmcentire at gmail dot com Reported By: jmcentire at gmail dot com Status: Bogus Bug Type: Feature/Change Request Operating System: Debian PHP Version: 5.3.1 New Comment: To whoever started this bug report(and the note in language.oop5.magic), you are using my email address...please use your own. Previous Comments: ------------------------------------------------------------------------ [2009-12-29 12:14:18] johannes@php.net gettype should really give the type, your class will always behave differently from an integer (just think about by value and by ref) ------------------------------------------------------------------------ [2009-12-29 00:35:42] jmcentire at gmail dot com 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 this bug report at http://bugs.php.net/?id=50599&edit=1

« previous php.bugs (#146195) next »