ID: 24687 Comment by: zhundiak at comcast dot net Reported By: nologic at pchome dot com dot tw Status: Open Bug Type: Zend Engine 2 problem Operating System: * PHP Version: 5CVS New Comment:
This bug pretty much rules out backwards compatibility with most php4 OOP programs. It's quite silly to say that returning a reference by calling another function that returns a reference is "bad" programing practice. And by the way, C++ has no problem with returning pointers. Just to be clear: function &getHomeTeam() { return $this->getTeam(TEAM_TYPE_HOME); } Really should work. Previous Comments: ------------------------------------------------------------------------ [2003-11-19 21:26:10] kain at kuht dot it and this? Fatal error: Only variables or references can be returned by reference in /usr/lib/php/DB/common.php on line 766 function &query($query, $params = array()) { if (sizeof($params) > 0) { $sth = $this->prepare($query); if (DB::isError($sth)) { return $sth; } $ret = $this->execute($sth, $params); $this->freePrepared($sth); return $ret; } else { $result = $this->simpleQuery($query); if (DB::isError($result) || $result === DB_OK) { return $result; } else { -->766 return new DB_result($this, $result); } } } running on Apache/2.0.48 (Trustix Secure Linux/Linux) PHP/5.0.0b2 ------------------------------------------------------------------------ [2003-11-17 02:35:55] mag at alcormizar dot cjb dot net I agree with Zeev on this one, this shouldn't be fixed. People will need to learn to write good code one day or the other, and returning a reference like that is VERY bad practice (try to do that in C++!). Even returning a reference to a local variable like this example should produce an error : class foo { function &bar() { $whatever = 5; return $whatever; } } because $whatever should not exist anymore after function bar return (according to correct scope rules), thus this reference is not referencing anything anymore... Instead of always asking to fix bad programming practice people should learn the correct way to do it. This is my opinion. ------------------------------------------------------------------------ [2003-08-16 04:14:19] jan at horde dot org Any idea yet if this will be fixed/addressed or should we start converting scripts to not use referenced return type or build variables that get returned? ------------------------------------------------------------------------ [2003-07-22 11:53:29] [EMAIL PROTECTED] Yes, it is quite complicated. You can only return variables by reference, a function's return value is not something we can 'connect' to... ------------------------------------------------------------------------ [2003-07-17 03:35:38] [EMAIL PROTECTED] If you do it like this it works: class TextSanitizer { function &htmlSpecialChars($text) { $foo = preg_replace("/&/i", '&', htmlspecialchars($text, ENT_QUOTES)); return $foo; } } So would it really be *that* hard to make it work? ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/24687 -- Edit this bug report at http://bugs.php.net/?id=24687&edit=1