Reference Gotcha

Through Planet PHP I saw the blog entry " Is PHP staying the language I want to work with? ", for with comments are cowardly disabled. Although the way classes are handled is debatable, moaning that PHP 4.4 breaks "return ($ret)" when returning by reference only shows that the programmer has had no clue about references in the first place. If you place () around a variable, you're making it an expression. You can only return variables by references, not expressions. The return-by-reference in this function never could have worked as it should have in the first place. Clue: Don't use "return (<something>)", but just "return <something>".

Comments

Heh :)

Comments do work.

So why can I do this [in PHP5]?

$a = new stdclass(); $a->val = "hello"; $c = ($a); print ($a === $c); // true

I think that PHP is far from consistent in this kind of stuff -- or the manual far from clear. You can use () with 'require', for instance, buty require also is not a function. Just as you can choose not use them with 'print' which is a function .... Maybe there's some deeply hidden pattern here that escapes us PHP users.

That's because $c = ($a) simply assigned the value of the expression "($a)" to $c.

Would you care to cite a source for your statement that the implementation is "correct"? I have never seen or heard of a specification for the PHP language. Therefore, the only "correct" implementation is whatever is implemented by the PHP developers. You can't even argue that the specification is vague on this point. There is nothing in the manual to suggest that () or ?: should produce copies; they simply do. This could, hypothetically, be changed in some future version of PHP. I can easily argue that () should not generate copies if there is only one term inside of them. It is counterintuitive and ineffecient. I could also argue that PHP should issue a warning/notice for this case.

I see little point in belittling people for not knowing the details of the implementation of language constructs, especially when the behavior is not specified in the only reference they have (the manual).

I should also note that the manual is incorrect about () being required when the value being returned is an expression. At least in PHP 4.3.11, the statement return 'hello ' . 'world'; works perfectly fine. The manual is probably just out of date (I am too lazy to compile and check older versions to determine if it was ever crrect), but it makes the point that there is no reliable source for correct PHP information other than the implementation you're using.

Add Comment

Name:
Email:

Will not be posted. Please leave empty instead of filling in garbage though!
Comment:

Please follow the reStructured Text format. Do not use the comment form to report issues in software, use the relevant issue tracker. I will not answer them here.


All comments are moderated

Life Line