Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 23rd, 2005, 3:02 AM   #1
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
PHP4 OOP Problem

I'm extremely confused, using PHP 4 i have set up a class named example with a constructor function inside of it to assign values to the class variables which are equal to NULL after execution and then there is an added variable NULL which is equal to the last value assigned to one of the class variables.

Here is the code i'm using, and the output it gives.

<?php
 class example {
 	var $test, $test1, $test2;
 
 	function example($sw) {
 		switch($sw) {
 			case 1:
 				$this->$test  = "1";
 				$this->$test1 = "2";
 				$this->$test2 = "3";
 			break;
 
 			case 2:
 				$this->$test  = "2";
 				$this->$test1 = "3";
 				$this->$test2 = "1";
 			break;
 
 			case 3:
 				$this->$test  = "3";
 				$this->$test1 = "1";
 				$this->$test2 = "2";
 			break;
 
 			default:
 				die("Error");
 			break;
 		}
 	}
 }
 
 $test = array(
 				new example(1),
 				new example(2),
 				new example(3)
 			 );
 
 var_dump($test);
 ?>

  array(3) {
    [0]=>
    object(example)(4) {
  	["test"]=>
  	NULL
  	["test1"]=>
  	NULL
  	["test2"]=>
  	NULL
  	[""]=>
  	string(1) "3"
    }
    [1]=>
    object(example)(4) {
  	["test"]=>
  	NULL
  	["test1"]=>
  	NULL
  	["test2"]=>
  	NULL
  	[""]=>
  	string(1) "1"
    }
    [2]=>
    object(example)(4) {
  	["test"]=>
  	NULL
  	["test1"]=>
  	NULL
  	["test2"]=>
  	NULL
  	[""]=>
  	string(1) "2"
    }
  }
__________________

tempest is offline   Reply With Quote
Old May 23rd, 2005, 4:37 AM   #2
Nuticulus
Programmer
 
Nuticulus's Avatar
 
Join Date: May 2005
Location: England
Posts: 61
Rep Power: 4 Nuticulus is on a distinguished road
Address class variables inside your class as:
$this->var_name
and not:
$this->$var_name

That should fix your problem.
Nuticulus is offline   Reply With Quote
Old May 23rd, 2005, 6:04 AM   #3
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
isnt OO better used in PHP5?
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity."

- Albert Einstein
Berto is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:49 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC