|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2009-12-29 12:14 UTC] johannes@php.net
[2009-12-29 17:16 UTC] jmcentire at gmail dot com
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 18:00:01 2025 UTC |
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.