473,396 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem setting object variables in PHP4

Hi,

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

<?php

// 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
various
values. I eventually discovered that every time I used a set function,
it
set every member variable to that value. For example, if I do
$obj_instance->setTop(5) on an object of type Macroblock, then $top,
$left,
AND diff are all set to 5. Does anyone know what is going on?
Thanks,
Erica
http://www.thanksforthemammaries.net

Jul 17 '05 #1
1 1565
On 23 Jan 2005 16:02:27 -0800, "erica" <er***@technodyke.com> wrote:
I am having a very strange problem. I wrote a program in php 4.3.10
that
uses objects. My classes.inc file looks like this:

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

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

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chung Leong | last post by:
Help me here. I've become a bitconfused about how PHP4 deals with objects after reading this description of PHP5: "PHP's handling of objects has been completely rewritten, allowing for better...
12
by: Berislav Lopac | last post by:
Is there a way for an object to unset itself in PHP4? Of course I could unset it from outside, but I would like to keep the code clean. The object has a method which deletes it's "footprint" in...
21
by: Thomas Mlynarczyk | last post by:
Hello, My provider has set register_globals = On and I can't change the php.ini file. Is there a way to unset all the imported get/post etc. variables at the beginning of my script? Thomas
12
by: Chung Leong | last post by:
I've been trying to set up a LAMP server for experimental purpose. Our production environment is WAMP (Windows Apache2 MSSQL PHP) but out of curiosity I would like to see if we can operate our...
9
by: Nicko | last post by:
Hey everyone, I'm on a server running PHP 5.0.5 but we've discovered that there's a problem with it. We cannot create a form with method "POST" because it simply will not post the form data...
1
by: KShapiro | last post by:
Hi All, I am trying to assist my wife, by making a simple menu system for the website. The web page is a .php4 file and does an include of the navigation file. Before it includes that file at...
0
by: cty | last post by:
Title: Problem in session using php5 Good day, I use php5+mySQL4+IIS5.x Previuosly i use php4 and no error occur,
10
by: olafilink | last post by:
Hello, we're running an Win2003server with IIS6 and PHP4 in fastCGI config. When i try to use system() command to execute ffmpeg.exe it works ok when i run the PHP script from the commandline on...
5
by: tshad | last post by:
In VS 2003, I am setting up an abstract class that is setting up classes for each datatype of VB.Net (as well as C#). I am trying to set it up so that most of the work is done in the Abstract...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.