View Full Version : Joining variables in a class

04-29-2009, 11:53 AM
Hi all
First off l best mention that classes are new to me and to be honest I'm a bit intimidate by the whole thing. Any way as a way to break myself into things (gently) I've been having a play at creating a class that will generate a sting from certain variables. You'll see what I mean from the code (that doesn't work!)

class xtags {

var $coord_x = 0;
var $coord_y = 0;
var $xtag;

function create_tag() {

$this->$xtag = $this->$coord_x . ',' . $this->$coord_y;

return $this->$xtag;


$test = new xtags();
$test->coord_x = 1;
$test_tag = $test->create_tag();

echo $test_tag;

Basically I need to generate a string that has two coordinates separated by a comma. 1,0 but what I get is ,

While you'll be right in saying the example doesn't justify being a class ultimately I'll want to set say 50 variables joined into a string but may only need to change one. Yes it could be done in a standard way but this is just to help me understanding the workings of a class.

Obviously it's this:
$this->$xtag = $this->$coord_x . ',' . $this->$coord_y;
that's not producing the result so how do you join the variables?


04-29-2009, 12:20 PM
You have way too many $ in there. The syntax is class::$static_variable and $instance->instance_variable, so

$this->$xtag = $this->$coord_x . ',' . $this->$coord_y;
return $this->$xtag;

really should be

$this->xtag = $this->coord_x . ',' . $this->coord_y;
return $this->xtag;

04-29-2009, 12:34 PM
Many thanks.

So basically $this-> omits the need for $ as '$this->stringname' is simply refering to the '$stringname' of the current object.