views:

86

answers:

1

Hi,

I was writing some php code after a long sint doing ruby and I accidently wrote this:

[root@ip-10-160-47-98 test]# cat run.php
<?php

class MyTest {

   public function run() {
      var_dump(this.test);
   }
}

$object = new MyTest();
$object->run();
[root@ip-10-160-47-98 test]# php run.php
string(8) "thistest"
[root@ip-10-160-47-98 test]#

Now, this.test should have been $this->test, but the compiler was actually happy to let this run.

Does anyone know how (this.test) got converted into a string "thistest"?

Compiled and run on php 5.3.2 amazon instance ami-e32273a6 (CentOS 5.4)

-daniel

+5  A: 

this and test are implicitly converted to strings, and . is the concatenation operator.

Matthew Flaschen
and '.' concatenates in php
Michael Haren
Ahh, the lovely implicit conversions in PHP! PHP never ceases to amaze me. I swear!
iconiK
@iconiK With appropriate error reporting active PHP should have thrown two errors a la "Undefined constant `this`, assuming string 'this'." Not that that's really any better, mind you, it's just more obvious. :o)
deceze