5 Replies - 717 Views - Last Post: 23 November 2009 - 07:10 PM Rate Topic: -----

#1 stv813  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 12
  • Joined: 31-March 09

Arrays in Classes

Post icon  Posted 23 November 2009 - 11:22 AM

I have the follow two (simplified) classes:
class Game
{
	var $DateTime;
	var $home_team;
	var $away_team;

	var $name;
}

class Team
{
	var $HomeGames;
	var $AwayGames;
}



And I have a (global) function (also simplified):
$GameCount = 0;
function AddGame($game)
{	
	global $GameCount;

	$GameCount++;
	$game->home_team->$HomeGames[$game->DateTime] = $game;
	$game->away_team->$AwayGames[$game->DateTime] = $game;
}



$game has valid home_team and away_team pointers (I am able to print the teams' names). I am also able to do a for-each on the arrays to see the key and value are added (and they are). However, when the function exits, the arrays are empty.

Is This A Good Question/Topic? 0
  • +

Replies To: Arrays in Classes

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon


Reputation: 7160
  • View blog
  • Posts: 14,924
  • Joined: 16-October 07

Re: Arrays in Classes

Posted 23 November 2009 - 11:45 AM

You'd want something like:

class Team {
	public $HomeGames = array();
	public $AwayGames = array();
}




I'd do something more like:
class Team {
	public $HomeGames = array();
	public $AwayGames = array();
	
	public function AddGame($game) {
		if ($this==$game->home_team) {
			$this->HomeGames[$game->DateTime] = $game;
		} else {
			$this->AwayGames[$game->DateTime] = $game;
		}
	}
}

class Games {
	public $gameList = array();
	
	public function AddGame($game) {
		$this->gameList[] = $game;
		$game->home_team->AddGame($game);
		$game->away_team->AddGame($game);
	}
}



Even that's rather poor, but at least the objects are now doing some work.

This post has been edited by baavgai: 23 November 2009 - 11:46 AM

Was This Post Helpful? 0
  • +
  • -

#3 stv813  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 12
  • Joined: 31-March 09

Re: Arrays in Classes

Posted 23 November 2009 - 11:59 AM

I'm running PHP4 (and I can't change that) so I use var instead of public which means everything is public so that's not the problem. Initializing with an empty array didn't seem to help either. It seems like a copy of something is being created somewhere. It must be doing an assignment by value instead of by reference somewhere. But all objects are stored as pointers, right? Arrays too?



Also, the classes do some work but it's not relevant to this question so I left it out.
Was This Post Helpful? 0
  • +
  • -

#4 stv813  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 12
  • Joined: 31-March 09

Re: Arrays in Classes

Posted 23 November 2009 - 12:05 PM

I just found out PHP4 sucks for objects/pointers so I'm going to have to go at this from a new angle.

Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon


Reputation: 7160
  • View blog
  • Posts: 14,924
  • Joined: 16-October 07

Re: Arrays in Classes

Posted 23 November 2009 - 12:53 PM

View Poststv813, on 23 Nov, 2009 - 01:05 PM, said:

I just found out PHP4 sucks for objects/pointers


This is quite true, I'm afraid.

What PHP in all flavors is good at is associative arrays. You can pretty much make a loose object system on your own terms with that. You can serialize those arrays into the session to have object persistence.

Good luck.
Was This Post Helpful? 0
  • +
  • -

#6 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 463
  • View blog
  • Posts: 820
  • Joined: 17-June 08

Re: Arrays in Classes

Posted 23 November 2009 - 07:10 PM

View Poststv813, on 23 Nov, 2009 - 12:59 PM, said:

It must be doing an assignment by value instead of by reference somewhere. But all objects are stored as pointers, right? Arrays too?

In PHP5 yes, objects are passed by reference by default. Arrays are not. And in PHP4, as you surmised, everything is passed by value. If you want to pass an argument by reference, as in this case, just add an ampersand to the function declaration, like this:
function AddGame(&$game) { ...

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1