11-08-2012, 09:27 PM
Hey there, I have another coding practice question (The PHP docs are so vague about some things). Here's what I mean:
Assuming the class constructor doesn't want any arguments passed PHP won't complain, even in strict mode. It's nice for readability.
Is omitting the parenthesis here discouraged, just preference, or nobody knows because PHP never mentioned it?
11-08-2012, 10:42 PM
Good question. I'd suggest you always use them; the docs do not specify that they are required, but they also do not specify them as optional in void argument constructors. Examples also show the use of them at all times. Hence it may be an unintended "feature" that will be removed in the future; 5.0 had some interesting "features" like being able to overwrite $this, static abstract methods, and several others. These have mostly been corrected by 5.2 and onwards.
I'm not sure why it lets you get away with this tbh. It could be an unintended feature they never bothered to resolve, or that it may be a requirement to not break backwards compatibility. I did find a remnant in the types section that show an example of using new without the parenthesis, but it appears to be an example in PHP4.x. So if that's the case (I didn't program OO in php 4.x, so I'm not all that familiar with the intricacies of it), than it would be for backwards compatibility.
11-08-2012, 11:20 PM
That's probably a good way to put it. If this is "unintended" functionality — like they made this work because it was simpler than handling it as an error — or backwards compatibility for PHP 4, then it's not very future-proof. Thanks for the input Fou-Lu, at least I know now it's not just my ignorance.
I'll consider this plausibly resolved, in that the minor benefits are outweighed by uncertainty.
11-09-2012, 06:30 AM
First example of the object initializing without parenthesis in the php manual http://php.net/manual/en/language.types.object.php
11-09-2012, 11:12 AM
Interesting.. Half the examples seem to use them without parenthesis, and that documentation page was updated on today.
11-09-2012, 04:59 PM
That's the same one I found.
Since I know for sure that it'll work in 4.x, I would presume that this is a BC issue.
Don't trust the documentation update dates either. It pretty much says all were updated on the current date.