2

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:

  1. Is $this actually implemented as a reference ((NOT a PHP reference, but a true pointer-esque reference--as in C++) "true reference") within PHP?
  2. 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);
        }
    }
    
  3. 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.

Community
  • 1
  • 1
Brandon S.
  • 306
  • 1
  • 4
  • 14
  • 7
    I suspect your fatal error is because you've not escaped the `$` in `echo "$this type: ...` so the interpreter is trying to convert the object to a string for output. As to whether it's a memory pointer (a la C), no : http://php.net/manual/en/language.references.whatare.php – CD001 Feb 22 '16 at 15:27
  • Maybe you can try with get_class($this); That would be helpfull to answer your question – Martin Verjans Feb 22 '16 at 15:32
  • Try `echo '$this type' . gettype(..)` instead, or `echo "\$this etc...`. Putting `$this` into a `"`-quoted string forces php to try and stringify that object, and it doesn't have a __toString() magic method. – Marc B Feb 22 '16 at 15:32
  • @CD001 Wow, totally missed that, thanks. -Martin. Thanks. So, it's definitely telling me that $this is an object of class FooFighter. But unfortunately, it doesn't quite answer the core of my question, as I'm fairly sure that a "true reference" would return the same values, as a "true reference" would point to a memory address. (Thanks for the info) – Brandon S. Feb 22 '16 at 15:36
  • @MarcB Thanks, Yes. I am aware of this, it was a typo made in haste. – Brandon S. Feb 22 '16 at 15:36
  • 1
    see http://docs.php.net/manual/en/language.oop5.references.php , "reference" means "object identifier which allows object accessors to find the actual object". – VolkerK Feb 22 '16 at 15:52

1 Answers1

3

Note the following:

php > $x = new StdClass();
php > $y = &$x;
php > echo gettype($x);
object
php > echo gettype($y);
object
php > class foo { function __construct() { echo gettype($this); } }
php > $z = new foo();
object
php > $a = 42;
php > $b = &$a;
php > echo gettype($b);
integer

Note that in no case will PHP report a variable as a reference. It only reports on the type of whatever the reference is pointing at.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Thanks, this gets much closer to the question I'm asking. So is there any way to know? I'm sure that an in-depth book on PHP might mention how the language is implemented, but I'm not quite ready for that reading. The information I have now is sufficient for me to continue work. So $this is a reference, like the manual says, but I can't help but be curious as to how it's implemented. Is there anywhere I can see the implementation of the PHP language? Google results are not working too good for me. – Brandon S. Feb 22 '16 at 15:41
  • there used to be a hack which dug into php's debug guts to count: http://stackoverflow.com/questions/4817562/detecting-whether-a-php-variable-is-a-reference-referenced doesn't work as of 5.4 however. and doesn't work any single generic variable-which-may-be-reference – Marc B Feb 22 '16 at 15:43
  • Thanks for that link, bummer it doesn't work past 5.4. Well. I'm leaving the question open for now, maybe someone who has dissected PHP before will know the answer to this. – Brandon S. Feb 22 '16 at 15:50