Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-08-2012, 08:27 PM   PM User | #1
Custard7A
Regular Coder

 
Custard7A's Avatar
 
Join Date: Jul 2010
Location: Australia
Posts: 269
Thanks: 32
Thanked 32 Times in 32 Posts
Custard7A is an unknown quantity at this point
Acceptable Object Instantizing

Hey there, I have another coding practice question (The PHP docs are so vague about some things). Here's what I mean:

PHP Code:

 
new Object
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?
Custard7A is offline   Reply With Quote
Old 11-08-2012, 09:42 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 11-08-2012, 10:20 PM   PM User | #3
Custard7A
Regular Coder

 
Custard7A's Avatar
 
Join Date: Jul 2010
Location: Australia
Posts: 269
Thanks: 32
Thanked 32 Times in 32 Posts
Custard7A is an unknown quantity at this point
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.
Custard7A is offline   Reply With Quote
Old 11-09-2012, 05:30 AM   PM User | #4
poyzn
Regular Coder

 
poyzn's Avatar
 
Join Date: Nov 2010
Posts: 265
Thanks: 2
Thanked 61 Times in 61 Posts
poyzn is on a distinguished road
First example of the object initializing without parenthesis in the php manual http://php.net/manual/en/language.types.object.php
__________________
Ushousebuilders.com
poyzn is offline   Reply With Quote
Old 11-09-2012, 10:12 AM   PM User | #5
Custard7A
Regular Coder

 
Custard7A's Avatar
 
Join Date: Jul 2010
Location: Australia
Posts: 269
Thanks: 32
Thanked 32 Times in 32 Posts
Custard7A is an unknown quantity at this point
Interesting.. Half the examples seem to use them without parenthesis, and that documentation page was updated on today.
Custard7A is offline   Reply With Quote
Old 11-09-2012, 03:59 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
classes, oop, syntax

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:28 AM.


Advertisement
Log in to turn off these ads.