So, I'm just curious about something theoretical that is also practically useful. This question is also an immediate followup to this SO question.
For the pseudo-variable "$this" in PHP, the manual has this to say:
$this is a reference to the calling object (usually the object to which the method belongs, but possibly another object
[words bolded for emphasis].
So my question is basically a multi-parter:
- Is $this actually implemented as a reference ((NOT a PHP reference, but a true pointer-esque reference--as in C++) "true reference") within PHP?
What does the manual mean by reference here? Is it saying that when I use the pseudo-variable $this (such as in the code sample below) that I am passing a variable of type 'object' into myFunction? Or, am I passing a PHP reference or a "true reference" to myFunction?
class FooFighter{ __construct(){ $this->myFunction($this); } function myFunction($foo){ gettype($foo); } }
I tried to find the answer to #2 by using the following code, but I get an error:
class FooFighter{ __construct(){ echo '$this type: ' . gettype($this); } }
Catchable fatal error: Object of class AppInstance could not be converted to string in file on line 44
This tells me that the variable that should be passed into myFunction is in fact an object, but I would expect to get the same behavior if I passed a "true reference" to myFunction, as a "true reference" (C++ Style Reference) can be used in place of the actual object.
So, is $this in PHP a C++ style reference ("true reference") or an actual object? I'm just a little confused by the documentation.