6 Replies - 10761 Views - Last Post: 14 September 2014 - 07:32 AM

#1 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Brilliance of PHP

Posted 11 July 2012 - 05:53 AM

Run and see!

<?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();


Is This A Good Question/Topic? 0
  • +

Replies To: Brilliance of PHP

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,226
  • Joined: 08-June 10

Re: Brilliance of PHP

Posted 11 July 2012 - 07:19 AM

seems like some kind of polymorphism to me.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6062
  • View blog
  • Posts: 23,514
  • Joined: 23-August 08

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. :P

EDIT : Is PHP determining, based on the method calls, to treat $this as an object in these cases?
Was This Post Helpful? 0
  • +
  • -

#4 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

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 :)

This post has been edited by RudiVisser: 11 July 2012 - 08:35 AM

Was This Post Helpful? 0
  • +
  • -

#5 IonutBajescu  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 13-September 14

Re: Brilliance of PHP

Posted 13 September 2014 - 01:40 PM

Ok, this is weird.

<?php

class ThisTest {
    public function SetAndUseThis() {
		${'th'.'is'} = new stdClass;

		var_dump(get_class($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();


Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3718
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Brilliance of PHP

Posted 13 September 2014 - 02:46 PM

Not really, though. If the logic RudiVisser suggests is what's really happening, getting stdClass for the first dump and the member values for the other two would make perfect sense.
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2983
  • Posts: 10,313
  • Joined: 08-August 08

Re: Brilliance of PHP

Posted 14 September 2014 - 07:32 AM

Yes, it seems to be creating a local variable called $this.
<?php
class ThisTest {
	public function test_this() {
		var_dump($this);
	}
	
    public function SetAndUseThis() {
		$var = 'this';
		$$var = 'that';
		var_dump($this);
		var_dump($this->variable);
		var_dump($this->AnotherMethod());
		$this->test_this();
	}
	
	public $variable = 'i am a variable';
	
	public function AnotherMethod()
	{
		return 9.0;
	}
}

$inst = new ThisTest();
$inst->SetAndUseThis();

I'd be wary of using it in anything, but it is interesting!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1