SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 35
  1. #1
    SitePoint Addict pachanga's Avatar
    Join Date
    Mar 2004
    Location
    Russia, Penza
    Posts
    265
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP 5.1 "Fatal error: Cannot re-assign $this"

    Guys, i'm totally confused with this fatal error which pops up in PHP 5.1. Have a look at the code snippet which triggers it(the same code runs ok in PHP 5.0.4):

    PHP Code:
    <?php

    class Foo {
      function 
    Foo() {
        
    $ref =& $this;
        
    $ref->test();
      }

      function 
    test() {
        echo 
    'test';
      }
    }

    $foo = new Foo();
    ?>
    This new behaviour of PHP 5.1 breaks a lot of BC with tons of PHP4 compatible code(WACT is one of them).

    Actually i would understand this behavior if PHP core developers wanted to protect $this from malicious usage(e.g $ref =& $this; $ref = "wow") but why not raising fatal error when it actually happens in the runtime??? The code i'm writing is absolutely valid and doesn't misuse PHP in any kind of this bad way.

    However even this "protection" can't handle this quick'n'dirty trick:
    PHP Code:
    <?php

    function & getRef(&$ref) {
      return 
    $ref;
    }

    class 
    Foo {
      function 
    Foo() {
        
    $ref =& getRef($this);
        
    $ref->test();
      }

      function 
    test() {
        echo 
    'test';
      }
    }

    $foo = new Foo();
    ?>
    Yahoo, it works just fine in PHP 5.1!

    But things get even merrier! Trying to clarify this situation i posted yet another "Fatal error: Cannot re-assign $this" bug report to Zend and this is the reply i got:

    [3 Sep 1:41pm CEST] helly@php.net

    Thank you for taking the time to write to us, but this is not
    a bug. Please double-check the documentation available at
    http://www.php.net/manual/ and the instructions on how to report
    a bug at http://bugs.php.net/how-to-report.php

    Objects are already references in PHP 5, thuse there is no sense in
    ...&$this in any way.

    [3 Sep 1:44pm CEST] derick@php.net

    Thank you for taking the time to write to us, but this is not
    a bug. Please double-check the documentation available at
    http://www.php.net/manual/ and the instructions on how to report
    a bug at http://bugs.php.net/how-to-report.php
    It seems like core devs don't spend much of their precious time reading contents of bug reports and simply paste the same reply template every time they see familiar bug report title and close it. This kind of attitude from PHP core developers seriously worries me

    I still do think it's a bug not a feature but it seems there's nothing i can do about it. Oh, thanks to ugly PHP internals, at least we can stick to this "getRef(&$ref)" trick

  2. #2
    SitePoint Guru momos's Avatar
    Join Date
    Apr 2004
    Location
    Belgium
    Posts
    919
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    remove the '&'

  3. #3
    SitePoint Addict pachanga's Avatar
    Join Date
    Mar 2004
    Location
    Russia, Penza
    Posts
    265
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by momos
    remove the '&'
    How about BC with PHP4?

  4. #4
    SitePoint Guru momos's Avatar
    Join Date
    Apr 2004
    Location
    Belgium
    Posts
    919
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    same thing goes for things like magic_quotes/register_globals... I'm guessing hosts will have some apps on PHP4 and more recent ones on PHP5... The same goes for the switch from xhtml 1.0 to xhtml 2.0.

    On the on hand it will stall people switching, but on the onther hand it you can really innovate...

  5. #5
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    Quote Originally Posted by pachanga
    This kind of attitude from PHP core developers seriously worries me
    It's a sick joke and I've long since given up submitting bugs for this reason.

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  6. #6
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lastcraft
    Hi...



    It's a sick joke and I've long since given up submitting bugs for this reason.

    yours, Marcus
    I submitted a bug once and I would think I spent about 2 or 3 full days of work convincing them it was actually a bug--but only after I finally contacted Zend directly and managed to get in contact with someone who actually understood the problem.

    So my desire for submitting more bugs is not strong. Perhaps if it's something really basic and obvious I'll do it again.
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Montreal
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you checked to see if the code behaves as expected when you remove the &? The logic is perhaps that $this is a reference already. Seems fishy to me. Again, its pretty strange to make such a dramatic change in a minor version... but that issue has already been beaten to the ground.

    Let me know if you find any clean solutions.

  8. #8
    get into it! bigduke's Avatar
    Join Date
    May 2004
    Location
    Australia
    Posts
    847
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dagfinn
    I submitted a bug once and I would think I spent about 2 or 3 full days of work convincing them it was actually a bug--but only after I finally contacted Zend directly and managed to get in contact with someone who actually understood the problem.
    Off topic:
    Same here, the 'cookies not being set' bug upon using a header call immediately after a setcookie call still exists. I reported this about a year back. Numerous versions have been released since, but ^&*#&@! I just happened to come across this one yet again. Thought someone must have worked it out by now, but nope.

    Frustrating!

    Bug Info

  9. #9
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Objects are already references in PHP 5, thuse there is no sense in
    ...&$this in any way.
    it's 'simply ridiculous. They have a code in zend_compile.c that handles "$this=$x" and copy-pasted that code in the function that comples assignment by reference. This should prevent "$this=&$x" (which is wrong), but for some reason it "prevents" "$x=&$this" as well (which is absolutely correct). It's pure c-level bug and has nothing to do with "new object model" and other blah-blah.

  10. #10
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As stated in another post. Zend really need to sort something out. I'm sure they have some trained chimps to copy and paste these silly messages. It seems you need to really bug Zend (excuse bad pun) to make them realise it's a bug.

    Like Marcus, I've given up, so much to the point I no longer use PHP.

    I posted this bug to PHP a long time ago (see: http://bugs.php.net/bug.php?id=33643&edit=1). And they said 'Won't fix'. I can't understand why. What's more is I reported this to SquirrelMail and they managed to fix this issue despite it being Zends fault! One day Zend will be wondering why everyone is using Python and Ruby. It's hardly suprising

  11. #11
    SitePoint Addict pachanga's Avatar
    Join Date
    Mar 2004
    Location
    Russia, Penza
    Posts
    265
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MiiJaySung
    One day Zend will be wondering why everyone is using Python and Ruby. It's hardly suprising
    This is really sad....we're outgrowing the language and they simply don't care.

    P.S. I wonder if zend core devs ever browse advanced forums like this one just to see what kind of problems mere developers face

  12. #12
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    Toronto
    Posts
    300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know. I think those folks get a lot of flack despite the fact that they do work very hard and are trying to do the right things. Sure, sometimes they get it wrong, but I don't think that this sort of appeal is going to help anyone. Remember, those folks have had to deal with a lot of bogus bugs from overzealous submitters who didn't always know better. I'm sure that it helps them develop a trigger finger. Still, sometimes it only requires more arm twisting and more lucid explanations to get your point across. I don't think that they aren't completely unreasonable people though I do empathize with your position and frustration. Just remember, you may be wrong too and/or they may have a very good reason for this behaviour.

    I don't know for sure but I wouldn't be surprised if this was to block certain uses of '$this' from static functions/classes and to block masquerading of $this from method parameters.

    It is interesting that this works fine:

    PHP Code:
    <?php 
      
     
    class Foo 
        var 
    $ref;
        function 
    Foo() { 
            
    $this->ref =& $this
            
    $this->ref->test(); 
        } 
      
        function 
    test() { 
            echo 
    'test'
        } 
     } 
      
     
    $foo =& new Foo(); 
     
    ?>


    Unfortunately, this only leads me to wonder why you would even be doing a ref trick like the one in your example. You are going to lose your local function variable when the method completes and $this is going to be available just as much as $ref is. So why use $ref when you can use $this directly? Even storing it as a class member makes no sense -- the object itself is already $this.

    I do agree that the regressions are getting painful but some of them only highlight the fact that PHP4 was being bent to do things that it shouldn't have been doing in the first place. For example, note that I used $foo =& new Foo(); -- whereas in your example, you used a simple = assignment. Without the & this leads to different behaviour under PHP4 than under PHP5 and this is documented in the manual.

    Personally I see this as more an issue regarding the fact that 100% BC is getting to be less likely than it used to be rather than being about bug reporting issues.

  13. #13
    SitePoint Addict pachanga's Avatar
    Join Date
    Mar 2004
    Location
    Russia, Penza
    Posts
    265
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jayboots
    It is interesting that this works fine:

    PHP Code:
    <?php 
      
     
    class Foo 
        var 
    $ref;
        function 
    Foo() { 
            
    $this->ref =& $this
            
    $this->ref->test(); 
        } 
      
        function 
    test() { 
            echo 
    'test'
        } 
     } 
      
     
    $foo =& new Foo(); 
     
    ?>
    Thanks, it works indeed! It's a weird workaround too but at least it looks way less strange than mine...

    Unfortunately, this only leads me to wonder why you would even be doing a ref trick like the one in your example.
    Sorry, you're right, my example looks very unnatural. Here's actually the code from WACT which needs a temporary reference to $this:

    PHP Code:
    [...]
        function &
    getRootDataSource() {
            
    $root =& $this;
            while (
    $root->parent != NULL) {
                
    $root =& $root->parent;
            }
            return 
    $root;
        }
    [...] 

  14. #14
    SitePoint Addict pachanga's Avatar
    Join Date
    Mar 2004
    Location
    Russia, Penza
    Posts
    265
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Derick has a bad day indeed - http://bugs.php.net/bug.php?id=34358

    It's still bogus, you can call it a BC break if you want, but we're not
    going to change this back.
    Last edited by pachanga; Sep 15, 2005 at 05:51. Reason: link changed

  15. #15
    SitePoint Enthusiast siteartwork's Avatar
    Join Date
    Jan 2005
    Location
    Germany
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pachanga
    Derick has a bad day indeed - http://bugs.php.net/bug.php?id=3435
    This points to a reasonable reply from Hartmut

  16. #16
    SitePoint Addict pachanga's Avatar
    Join Date
    Mar 2004
    Location
    Russia, Penza
    Posts
    265
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  17. #17
    SitePoint Enthusiast siteartwork's Avatar
    Join Date
    Jan 2005
    Location
    Germany
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pachanga
    [offtopic_rant]
    Ever taken a look at the zend engine? Looks horrible. No wonder one hand doesn't know what the other is doing. If they can't handle things like that, they should have done a complete CB rather than allowing PHP4 to run under ZE2... this way, they might have also fixed the parameter order flaws of some functions...
    [/offtopic_rant]

  18. #18
    SitePoint Addict pachanga's Avatar
    Join Date
    Mar 2004
    Location
    Russia, Penza
    Posts
    265
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    it's 'simply ridiculous. They have a code in zend_compile.c that handles "$this=$x" and copy-pasted that code in the function that comples assignment by reference. This should prevent "$this=&$x" (which is wrong), but for some reason it "prevents" "$x=&$this" as well (which is absolutely correct). It's pure c-level bug and has nothing to do with "new object model" and other blah-blah.
    Could you please point out exactly where in zend_compile.c it happens? Does this patch fix it?
    Index: Zend/zend_compile.c
    ===================================================================
    RCS file: /repository/ZendEngine2/zend_compile.c,v
    retrieving revision 1.647.2.4
    diff -u -p -d -r1.647.2.4 zend_compile.c
    --- Zend/zend_compile.c 9 Sep 2005 06:48:47 -0000 1.647.2.4
    +++ Zend/zend_compile.c 15 Sep 2005 14:53:39 -0000
    @@ -2062,7 +2062,7 @@ static zend_bool do_inherit_property_acc
    }
    if (zend_hash_find(ht, prot_name, prot_name_length+1, (void**)&prop) ==
    SUCCESS) {
    zval **new_prop;
    - if (zend_hash_find(ht, child_info->name,
    child_info->name_length+1, (void**)&new_prop) == SUCCESS) {
    + if (zend_hash_find(ce->static_members, child_info->name,
    child_info->name_length+1, (void**)&new_prop) == SUCCESS) {
    if (Z_TYPE_PP(new_prop) != IS_NULL && Z_TYPE_PP(prop)
    != IS_NULL) {
    char *prop_name, *tmp;
    zend_unmangle_property_name(child_info->name,
    &tmp, &prop_name);
    I'm asking about it because i just discovered this patch in the dev. internals list and i suspect PHP developers are silently fixing it....

  19. #19
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, I thnk it's in zend_do_assign_ref (around line 586)
    Code:
    		if (opline_is_fetch_this(last_op TSRMLS_CC)) {
    			zend_error(E_COMPILE_ERROR, "Cannot re-assign $this");
    		}

  20. #20
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didn't read the whole thread, but the OP is just confirming my thoughts:

    PHP has started as a simple Web dev platform with a scripting language. It has since been growing, and is now a teenager who keeps getting into trouble with his parents.

    Soon he'll have to leave the home abd start on his own -- i.e. PHP as a language should have to end being tied to its Web dev roots and become a full-fledged language -- or it might go mad and lose all his friends except the most loyal ones.

  21. #21
    SitePoint Addict pachanga's Avatar
    Join Date
    Mar 2004
    Location
    Russia, Penza
    Posts
    265
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks to Jeff Moore's blog post regarding to this problem Zend developers finally decided to fix it. The bug was reopened and given much more careful attention in the internals dev. list! Let's see what happens next...
    Last edited by pachanga; Oct 4, 2005 at 01:46.

  22. #22
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It seems to be FIXED in 5.1 cvs.

    Pavel, respect for your persistence and courage! It was a real adventure to make them fix it.

  23. #23
    SitePoint Addict pachanga's Avatar
    Join Date
    Mar 2004
    Location
    Russia, Penza
    Posts
    265
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heh, i think we all need even more persistence and courage to make them fix even nastier and more serious bugs

    I think if some additional publicity is given to the most notorious PHP bugs they will be fixed eventually.

    Marcus, Jeff, Harry - guys with your help and authority i'm sure we can make it.

  24. #24
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure how much was really due to the posting on Jeff's blog. At a similar time there was this discussion going on regarding a patch which had already been applied to the PHP source:
    here
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  25. #25
    SitePoint Guru
    Join Date
    Nov 2002
    Posts
    841
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It ain't fixed yet. Their still debating rolling back the fix. My blog post did pre-date the internals thread and the bug status switch by about 8 hours.

    There was some good work on this thread diagnosing the issue and developing a workaround.


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
  •