snapsvg

2013-03-28

Cannot reassign $this?

PHP won't let you reassign $this because it is a readonly reference to the contextual object.


class Test {
    var $foo = "bar";

    function abc() {
        $this = "Hello!";
    }
}

$test = new Test();
$test->abc();

PHP Fatal error:  Cannot re-assign $this in - on line 14

Fatal error: Cannot re-assign $this in - on line 14

Since PHP has the flaw of allowing you to use variables as variables, however, you can put 'this' in a variable. PHP doesn't seem to notice that that's assigning to $this:


class Test {
    var $foo = "bar";

    function abc() {
        $thisStr = "this";
        $$thisStr = "Hello!";
    }
}

$test = new Test();
$test->abc();

Tricked you!


However, if you then use $this as an object, it's still $this! But if you don't, it's not:


class Test {
    var $foo = "bar";

    function abc() {
        var_dump($this);
        var_dump($this->foo);

        $thisStr = "this";

        $$thisStr->foo = "cats";

        $$thisStr = "Hello!";

        var_dump($this);
        var_dump($this->foo);
    }
}

$test = new Test();
$test->abc();

class Test#1 (1) {
  public $foo =>
  string(3) "bar"
}
string(3) "bar"
string(6) "Hello!"
string(4) "cats"

Surely someone's realised by now that the people behind PHP might not really know what they're doing.


Edit: turns out this already came from reddit

2013-03-08

Gnu Passive-Aggressive Public Licence (GPAPL)

This licence is for people reluctant to give away code but who understand the rationale behind it.

This program is free software. I wouldn't expect you to pay for it anyway.
You can and probably will redistribute it and/or modify it under the terms 
of the GNU General Public License as painstakingly published by the Free 
Software Foundation, either version 3 of the License, or (at your option, 
if you think that's best) any later version. I know you're going to anyway
so I might as well legitimise it. 

This program is distributed in the hope that it will be useful, not that you 
care, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, especially yours.  
If you can find the energy, see the GNU General Public License for more 
details, but it's OK if you don't. Most people don't. Whatever.

You should have received a copy of the GNU General Public License
along with this program.  If not, all I can really do is point you to this URL 
<http://www.gnu.org/licenses/> and assume you're going to follow it. I can't
force you so whatever. Just have it. 

The GPAPL is released under the WTFPL, as if you care.