Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder Custard7A's Avatar
    Join Date
    Jul 2010
    Location
    Australia
    Posts
    286
    Thanks
    32
    Thanked 33 Times in 33 Posts

    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?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #3
    Regular Coder Custard7A's Avatar
    Join Date
    Jul 2010
    Location
    Australia
    Posts
    286
    Thanks
    32
    Thanked 33 Times in 33 Posts
    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.

  • #4
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    First example of the object initializing without parenthesis in the php manual http://php.net/manual/en/language.types.object.php

  • #5
    Regular Coder Custard7A's Avatar
    Join Date
    Jul 2010
    Location
    Australia
    Posts
    286
    Thanks
    32
    Thanked 33 Times in 33 Posts
    Interesting.. Half the examples seem to use them without parenthesis, and that documentation page was updated on today.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •