Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why did this work? ( php dot notation )

Tags:

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() {
      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

like image 610
Daniel Avatar asked May 25 '10 03:05

Daniel


People also ask

What is the use of period character in PHP?

It's the concatenation operator, concatenating both strings together (making one string out of two separate strings).

What is dot notation in PHP?

Dot provides an easy access to arrays of data with dot notation in a lightweight and fast way. Inspired by Laravel Collection. Dot implements PHP's ArrayAccess interface and Dot object can also be used the same way as normal arrays with additional dot notation.


1 Answers

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

like image 130
Matthew Flaschen Avatar answered Oct 13 '22 01:10

Matthew Flaschen