开发者

Why did this work? ( php dot notation )

开发者 https://www.devze.com 2022-12-31 17:45 出处:网络
I was writing some php code after a long sint doing rubyand I accidently wrote this: [root@ip-10-160-47-98 test]# cat run.php

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() {
  开发者_JS百科    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


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


php search for constant this and constant test, it doent find them so raise an Exception and convert this and test to 'this' and 'test' and joing them (dot is used to join strings)

0

精彩评论

暂无评论...
验证码 换一张
取 消