Problem setting object variables in PHP4

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I am having a very strange problem. I wrote a program in php 4.3.10
uses objects. My file looks like this:


// Coordinate class
class Coord {

// The variables
var $x;
var $y;

// Get functions to return values
function getX() { return $this->$x; }
function getY() { return $this->$y; }

// Set functions force input to an integer
function setX($int) { $this->$x = round($int); }
function setY($int) { $this->$y = round($int); }


// Macroblock class
class Macroblock {

// The variables
var $top;
var $left;
var $diff;

// Get functions to return values
function getTop() { return $this->$top; }
function getLeft() { return $this->$left; }
function getDiff() { return $this->$diff; }

// Set functions to insert values
function setTop($int) { $this->$top = round($int); }
function setLeft($int) { $this->$left = round($int); }
function setDiff($int) { $this->$diff = round($int); }



I started getting some strange results in my program when I echoed
values. I eventually discovered that every time I used a set function,
set every member variable to that value. For example, if I do
$obj_instance->setTop(5) on an object of type Macroblock, then $top,
AND diff are all set to 5. Does anyone know what is going on?

Re: Problem setting object variables in PHP4

Quoted text here. Click to load it

 You're accidentally using variable variables, and accessing an object
attribute with a null name (since $x is null) in all the setters and getters.
You need:

function getX() { return $this->x; }

 i.e. lose the $ before x.

< Space: disk usage analysis tool

Site Timeline