SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast konsama's Avatar
    Join Date
    Jun 2007
    Location
    Sweden
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ...the =& operator with the 'new' statement. It is deprecated

    Some IT guy told me this once.
    I know that in PHP5 you shouldn't use the =& operator with the 'new' statement. It is deprecated.
    Can I just delete the all "&" character wherever I see or will that cause chain effects which I need deeper understanding in order to handle correctly?

    For instance can I just delete "&" after the "=" character without causing troubles or should I let them be? (I'm a newbie PHP programmer.)
    // Instantiate the QuickForm class
    LINE 73.... $form =& new HTML_QuickForm('regForm', 'POST');
    LINE 74.... $renderer =& $form->defaultRenderer();

  2. #2
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are now using PHP 5 you can delete the & from your object creation lines and there shouldn't be any problems at all.

  3. #3
    SitePoint Enthusiast konsama's Avatar
    Join Date
    Jun 2007
    Location
    Sweden
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this line considered as an object creation line?
    Code:
    LINE 74.... $renderer =& $form->defaultRenderer();
    Or should I ONLY delete every "&" before all object creation lines containing the keyword "new"?
    Code:
    LINE 73.... $form =& new HTML_QuickForm('regForm', 'POST');

  4. #4
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In PHP 5 you do not have to assign objects by reference any more since they are always assigned / passed by reference.

    You could leave the =& for now , php5 will ignore it. (I hear in php 6 they might issue an error for it )

    Code:
    LINE 74.... $renderer =& $form->defaultRenderer();
    From the look of it , does not look like object creation line ( unless inside that function object is created and passed back )

    Most likely it is passing the results of the function by reference , most of the times it used to optimize the code and save on memory.

    In your case removing the & from the line could possibly have undesired effects , hard to tell without looking at the whole code.

    I suggest looking up tutorials on references to learn more about them.
    IQStaffing - IT Recruiter
    - Need an IT job? let us find you one!
    http://www.iqstaffing.com

  5. #5
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $form->defaultRenderer() return an object of renderer class (used for templating). The renderer object created this way is owned by form object and when you change the properties of the renderer objects it's reflected in the way the form is displayed. Therefore, 'pass-by-reference' is crucial here else $renderer object that you just created would no longer point to the render object inside the form. Consequently, no matter what you do with the $renderer object, you wont be able to see the effect on the form display.

  6. #6
    SitePoint Enthusiast konsama's Avatar
    Join Date
    Jun 2007
    Location
    Sweden
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is HEAVY. Tnx for your advises, I'll try and read up on references when I get that far.

  7. #7
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As a general rule of thumb, you can remove the ampersands. In 99% of the places, they are used to pass objects by reference in PHP4, which would otherwise clone the object instance. There are however some rare cases, where references are used for variables, which aren't objects. Removing the ampersands in these cases would break your code. So you do need to know if the variable being assigned, contains an object or something else.

  8. #8
    SitePoint Enthusiast konsama's Avatar
    Join Date
    Jun 2007
    Location
    Sweden
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are however some rare cases, where references are used for variables, which aren't objects. Removing the ampersands in these cases would break your code. So you do need to know if the variable being assigned, contains an object or something else.
    In that case I'll keep most of the PEAR class files and Harry Fuecks' class files intact. Why fix something that's not breaken ay.

  9. #9
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by konsama View Post
    In that case I'll keep most of the PEAR class files and Harry Fuecks' class files intact. Why fix something that's not breaken ay.
    That's probably a sound approach. There is no downside to keeping them, expect perhaps that the code is a little more cryptic to read.


Bookmarks

Posting Permissions

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