Anthop Posted January 4, 2008 Share Posted January 4, 2008 I'm not even sure what to call this problem as I seem to be doing everything correctly (though, of course, I'm still very new at this). I wrote a very simple test script to see what I could do with classes in PHP. The wrong values seem to be stored in the instance variables. Incidentally, I'm using PHP 4 (ugh >P). Here is the code: <?php class Constructor { var $variable1; var $variable2; function Constructor() { $num_args = func_num_args(); $arg_list = func_get_args(); if($num_args >= 1) {$this->$variable1 = $arg_list[0];} if($num_args >= 2) {$this->$variable2 = $arg_list[1];} else {$this->$variable2 = 10;} } function CreateNewStatically() {return new Constructor(2, 9);} function display() { echo $this->$variable1; echo ", "; echo $this->$variable2; } } $p1 = new Constructor(1); $p1->display(); echo("<br />"); $p2 = Constructor::CreateNewStatically(); $p2->display(); echo("<br />"); $p3 = new Constructor(); $p3->display(); echo("<br />"); ?> What I get back is the following: 10, 10 9, 9 10, 10 What I expect to get is: 1, 10 2, 9 , I tested it a bit and I found that the code is being traversed correctly and that there seems to be no problem with the variable number of variables to the constructor. I'm at a complete loss as to why I'm getting these values. I'm guess that this has to do with some facet of PHP that I don't know about. I would be grateful for any help! Thank you . Quote Link to comment https://forums.phpfreaks.com/topic/84429-solved-constructor-stores-wrong-values-to-instance-variables/ Share on other sites More sharing options...
redbullmarky Posted January 4, 2008 Share Posted January 4, 2008 when you're dealing with class properties, you don't need to use two $ - so instead of: $this->$variable1 use $this->variable1 giving the following output: 1, 10 2, 9 , 10 full code: <?php class Constructor { var $variable1; var $variable2; function Constructor() { $num_args = func_num_args(); $arg_list = func_get_args(); if($num_args >= 1) { $this->variable1 = $arg_list[0]; } if($num_args >= 2) { $this->variable2 = $arg_list[1]; } else { $this->variable2 = 10; } } function CreateNewStatically() { return new Constructor(2, 9); } function display() { echo $this->variable1; echo ", "; echo $this->variable2; } } $p1 = new Constructor(1); $p1->display(); echo("<br />"); $p2 = Constructor::CreateNewStatically(); $p2->display(); echo("<br />"); $p3 = new Constructor(); $p3->display(); echo("<br />"); Quote Link to comment https://forums.phpfreaks.com/topic/84429-solved-constructor-stores-wrong-values-to-instance-variables/#findComment-430127 Share on other sites More sharing options...
aschk Posted January 4, 2008 Share Posted January 4, 2008 Just to give you some insight to what you were doing before. You were attempting (accidently) to do a variable substitution when you wrote : $this->$variable1 . "What?" I hear you say. Simple... imagine the following: $variable1 = "silly"; echo $this->$variable1; In the above situation, $variable1 in the "$this->" part of your code attempts a substitution. e.g. what you have actually done is this: echo $this->silly; PHP can be a messy language sometimes but it makes us better programmers for knowing it's ins and outs. Quote Link to comment https://forums.phpfreaks.com/topic/84429-solved-constructor-stores-wrong-values-to-instance-variables/#findComment-430176 Share on other sites More sharing options...
Anthop Posted January 4, 2008 Author Share Posted January 4, 2008 Thank you! I certainly feel a bit silly now for not knowing that. I still don't quite understand why those particular values would be set at all. (By dereferencing $variable1, I was basically trying to store the value to a variable specified by the value of variable1.... But $variable1 itself was never set, so you'd think I would be trying to store to $this->null or something, and nothing would happen.... ???) Mysterious indeed. But in any case, thanks for clearing that up! ... Now to go back and change all the instance variable references in my code .... Quote Link to comment https://forums.phpfreaks.com/topic/84429-solved-constructor-stores-wrong-values-to-instance-variables/#findComment-430334 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.