Re: OO purism sucks - this is a rant for anyone who is allergic to that kind of thing...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Robert Cummings wrote:

Well there "was" a segfault, but that's fixed now, now the warning is
thrown because of some purist attitude that says it's incorrect to pass
a literal to a reference expecting parameter... I can see how that's an
issue in C with pointers, or in a strongly type language like Java, but
part of PHP's popularity is that while it shares a lot of functions with
C, it is neither C nor Java, but rather a much simpler system that
generally for all the novices out there "works well".

Stuff like this is a bug in your code.  It has nothing to do with purism:

function inc_arg(&$arg) {
  $arg++;
}
inc_arg(3);

You have specifically defined your function to modify the argument yet you pass it something that has no storage and thus there is no way to use that modification. For the most part I agree that we need to keep the language as flexible and forgiving as possible, but where we are able to detect code that obviously doesn't do what the programmer intended, it is tremendously helpful to get a descriptive error.

As far as functions returning references, you are right that returning a literal there sometimes makes sense, but this can also hide a bug which is why this only throws a notice.

You could argue that we could try to look ahead and see what is actually done with the passed argument to see if it is modified, or that we could try to figure out whether the returned reference is actually used as such, but that is technically extremely difficult given the way things work internally. So given the choice between ignoring extremely suspicious code like this and throwing an error/warning, I'd rather be told about it and in the edge cases where I really do want the equivalent of a NOP in my code, it isn't that hard to force it by adding a dummy storage variable along with a comment explaining why you need to do that.

-Rasmus

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux