<?php
class ThisTest {
public function SetAndUseThis() {
$var = 'this';
$$var = 'that';
var_dump($this);
var_dump($this->variable);
var_dump($this->AnotherMethod());
}
public $variable = 'i am a variable';
public function AnotherMethod()
{
return 9.0;
}
}
$inst = new ThisTest();
$inst->SetAndUseThis();
Brilliance of PHP
Page 1 of 13 Replies - 3544 Views - Last Post: 11 July 2012 - 08:25 AM
#1
Brilliance of PHP
Posted 11 July 2012 - 05:53 AM
Run and see!
Replies To: Brilliance of PHP
#2
Re: Brilliance of PHP
Posted 11 July 2012 - 07:19 AM
seems like some kind of polymorphism to me.
#3
Re: Brilliance of PHP
Posted 11 July 2012 - 07:58 AM
Hmmm, what's the rational analysis of the behavior? If $this is a string, how are methods being called on it? I avoid variable variables if at all possible. 
EDIT : Is PHP determining, based on the method calls, to treat $this as an object in these cases?
EDIT : Is PHP determining, based on the method calls, to treat $this as an object in these cases?
#4
Re: Brilliance of PHP
Posted 11 July 2012 - 08:25 AM
Well, you can't reassign $this directly. The only thing I can assume is that the actual value of $this which technically can never be set, is being completely ignored, and just being treated as a magic instance references in all cases.
The fact that an arbitrary $this was "created" in this scope would allow it to be used as what it was assigned to. Some form of logic based on "Does this variable exist? If not, is it a magic variable? If not, throw unassigned error." would rationalise this a little bit.
I mean, $this->method() would not be defined and would go back to the OOP name resolution, realising $this as a "magic" variable pointing to the current class instance. In the case of looking at $this directly, it is defined.
I don't know why, just the best thing I can come up with. Maybe it's time to take a delve into the PHP source
The fact that an arbitrary $this was "created" in this scope would allow it to be used as what it was assigned to. Some form of logic based on "Does this variable exist? If not, is it a magic variable? If not, throw unassigned error." would rationalise this a little bit.
I mean, $this->method() would not be defined and would go back to the OOP name resolution, realising $this as a "magic" variable pointing to the current class instance. In the case of looking at $this directly, it is defined.
I don't know why, just the best thing I can come up with. Maybe it's time to take a delve into the PHP source
This post has been edited by RudiVisser: 11 July 2012 - 08:35 AM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|