SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Assigning the return value of new by reference is deprecated...

    I recently upgraded my server and now I'm receiving the following error:

    "Assigning the return value of new by reference is deprecated in..."

    From what little I've so far about this, a lot of people claim that it can be fixed by simply removing the reference operator (&).

    Is this true? What's your experience in this?

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP 4 introduced basic OOP to PHP, however it had a few key issues which were fixed in PHP 5.

    Bear in mind that OOP is a concept which has feature which should be consistent between the many different programming languages which implement it.

    In most programming languages, an object can be passed to functions or other objects by reference, not by value. In other words, rather than passing a new object with the same values, the actual object is passed, meaning changes are kept.

    Here's an example of how it is in PHP 5:
    PHP Code:
    class Person{
        public 
    $Name;
    }
    function 
    Something(Person $Person){
        
    $Person->Name 'Jimbob';
    }
    $A = new Person();
    $A->Name 'James';
    Something($A);
    echo 
    $A->Name//Jimbob 
    Though, in PHP 4, the same code (IIRC) would output 'James', as the function recieves only a copy of $A.
    PHP Code:
    class Person{
        var 
    $Name;
    }
    function 
    Something($Person){
        
    $Person->Name 'Jimbob';
    }
    $A = new Person();
    $A->Name 'James';
    Something($A);
    echo 
    $A->Name//James 
    Because of this, you had to manually pass the object by reference rather than by value:
    PHP Code:
    class Person{
        var 
    $Name;
    }
    function 
    Something($Person){
        
    $Person->Name 'Jimbob';
    }
    $A = new Person();
    $A->Name 'James';
    Something(&$A);
    echo 
    $A->Name//Jimbob 
    However, if you do that in PHP5 you're trying to send a reference by reference, which doesn't really make much sense.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Basically, it's done automatically now, so remove the & and it'll work as before

  4. #4
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Having you guys explain this begs the question, "Why didn't they fix this sooner"?

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Wolf_22 View Post
    Having you guys explain this begs the question, "Why didn't they fix this sooner"?
    They fixed it in 2004... with the introduction of PHP 5
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    There was nothing to 'fix' as this was the agreed solution for PHP4.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


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
  •