Jump to content

[SOLVED] Constructor Stores Wrong Values to Instance Variables


Anthop

Recommended Posts

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

Link to comment
Share on other sites

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 />");

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 -_-....

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.