olegk Posted August 28, 2006 Share Posted August 28, 2006 This code prints out "AB", and it should print out "AA". What's wrong with me?PHP 4.4.2 <?phpclass test{ var $a=""; var $b=""; function testfunc() { $this->$a="A"; echo $this->$a; $this->$b="B"; echo $this->$a; }}$t=new test;$t->testfunc();?> Quote Link to comment https://forums.phpfreaks.com/topic/18957-strange-problems-with-php4-oop/ Share on other sites More sharing options...
sanfly Posted August 28, 2006 Share Posted August 28, 2006 [quote]function testfunc() { $this->$a="A"; echo $this->$a; $this->$b="B"; [color=red]echo $this->$a;[/color] }[/quote]should the line in red be:[code]echo $this->$b;[/code] Quote Link to comment https://forums.phpfreaks.com/topic/18957-strange-problems-with-php4-oop/#findComment-81948 Share on other sites More sharing options...
olegk Posted August 28, 2006 Author Share Posted August 28, 2006 [quote author=sanfly link=topic=106006.msg423695#msg423695 date=1156808950]...should the line in red be:...[/quote]No, that's the point. If it were $this->$b, I would expect the code to print "AB", but I print out $this-$a twice, and for some reason the second time it turns into "B" Quote Link to comment https://forums.phpfreaks.com/topic/18957-strange-problems-with-php4-oop/#findComment-81950 Share on other sites More sharing options...
drkstr Posted August 29, 2006 Share Posted August 29, 2006 This might be with PHP 5, but I think you need to access your vars with:[code] $this->a="A"; echo $this->a; $this->b="B"; echo $this->a;[/code]regards,...drkstr Quote Link to comment https://forums.phpfreaks.com/topic/18957-strange-problems-with-php4-oop/#findComment-81984 Share on other sites More sharing options...
wildteen88 Posted August 29, 2006 Share Posted August 29, 2006 To access your vars that is in the same class/function you need to use $this->[b]youVarNameHere[/b] just as drkstr pointed out above. This is the same for PHP4 and PHP5.You do not put the dollar sign after $this-> you just the name of the variable, ie $this->a Quote Link to comment https://forums.phpfreaks.com/topic/18957-strange-problems-with-php4-oop/#findComment-82063 Share on other sites More sharing options...
Jenk Posted August 29, 2006 Share Posted August 29, 2006 Is there any reason you didn't take this answer from the other forum you posted this on? Quote Link to comment https://forums.phpfreaks.com/topic/18957-strange-problems-with-php4-oop/#findComment-82109 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.