Php error: cannot assign values to 'this'




  • I found this in a php open source package, which I won't disclose.  Amazingly enough, in the phpDoc comments, the author was proud of this and boasting of their l33t skills.  It horrified me that someone took the time to document a hack like this and was proud on top of it.

    function myFunction($myObject, ... other params)
    {
        ...Code code Code
        //WTF?
        $this &= $myObject;
    }

    I can kind of see how someone would even think of using this, but it still scares me because it had a function name that gave you no warning that the class object being passed to the method could possibly be assigned to itself.  Further research into this class and its bastard method gave no warning of this either or any mechanism for comparing one class with another.

    I found this when I got a php error stating cannot assign values to 'this'.  I originally  thought maybe there was a bug or something and the programmer had forgotten the class property.  Nope.



  • @Ion9 said:


    I found this in a php open source package, which I won't disclose.

    It doesn't have a .no domain name by any chance, does it?

    function myFunction($myObject, ... other params)
    {
        ...Code code Code
        //WTF?
        $this &= $myObject;
    }

    Could you clarify exactly what that does?  I don't know PHP all that well, and I didn't manage to figure much out by experimenting.



  • [code]$this &= $myObject;[/code]

    &= is the dereference-assignment operator.

    It will copy the object contained by or referred to by $myObject into the variable $this, if I'm right.

    I wasn't aware that the this-keyword has variable-syntax in PHP.

    Yeah, dynamic programming takes a whole new level with PHP...


    $$varname;

    create_function('funcname','funccontents');



  • @dhromed said:

    &= is the dereference-assignment operator.


    I'm not sure if the original post was a typo, but &= is the bitwise AND operator. For example:

    [code]
    $a = 3;
    $a &= 2;
    print "$a\n";
    [/code]

    will output '2'.

    I have no idea what would happen if you used logical AND to modify $this with another random object ...



  • @craiga said:

    I have no idea what would happen if you used logical AND to modify $this with another random object ...


    OK, so I had to try it didn't I ... it sets $this to an integer 0. Wonderfully, this also affects the variable in the scope outside of the method - the variable on which the method was called is no longer an object. Nice.

    =& is the reference assignment operator, btw.



  • @craiga said:

    @dhromed said:
    &= is the dereference-assignment operator.


    I'm not sure if the original post was a typo, but &= is the bitwise AND operator. For example:

    [code]
    $a = 3;
    $a &= 2;
    print "$a\n";
    [/code]



    I'm drunk.

    The & is related to reference in in a totally different way.

    That'll teach me to not look things up.



  • It's =& and yeah, this is a smelly smelly hack.

    It's also become illegal in PHP5 so that explains why you are getting that error.



  • @craiga said:

    Wonderfully, this also affects the variable in the scope outside of the method - the variable on which the method was called is no longer an object. Nice.

    I was afraid it would be something like that. :-(


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.