6 Replies - 597 Views - Last Post: 06 June 2014 - 10:20 AM Rate Topic: -----

#1 DauntlessRob   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 22-September 13

Accessing Variables outside of a Class

Posted 04 June 2014 - 10:09 AM

I've been developing a browser-based game about spaceships and such in order to teach myself more about PHP and programming as a whole. So far, the entire game is built procedurally, which seems to work for it. However I'd like to learn about OOP as well, and so I'm trying to build the battle system between players in a class.

<?php 
	require_once('gen_authUser.php');  // checks if the user is logged in.
	require_once('gen_config.php');    // puts in database credentials.
	require_once('gen_functions.php'); // provides all my custom functions.
	require_once('gen_variables.php'); // *important* Does a sql query and creates $name among many others.
	
class Battle {
	public function __construct() {	
		echo "Your name is ".$name.".";
	}

}
	$battle = new Battle;
?>



It seems like this should be a simple thing to do, but I believe I'm missing something super basic, because obviously this doesn't work.

I've considered 2 options:

1) Open a new database connection and query all the info all over again within the class.
2) Set all the variables in gen_variables.php to PUBLIC (would that work? is it a bad idea?)

Any guidance you can offer to this novice programmer would be much appreciated. (also, I've been up for nearly 24 hours now so I appologize if any of this is nonsense.)

Is This A Good Question/Topic? 0
  • +

Replies To: Accessing Variables outside of a Class

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Accessing Variables outside of a Class

Posted 04 June 2014 - 10:18 AM

What is in gen_variables.php? Is it a class? If so, pass that class to the constructor of Battle. This is a "best practice" for OOP and is known as Dependency Injection.

But you should be off getting some sleep now.
Was This Post Helpful? 1
  • +
  • -

#3 DauntlessRob   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 22-September 13

Re: Accessing Variables outside of a Class

Posted 04 June 2014 - 10:41 AM

Madness!

No, it's not a class. Like i said, everything is very procedural right now. gen_variables is essentially a large while statement that breaks out a sql table's row into individual variables for the rest of the site to use as it needs them.

Now that I've read that excellent intro to Dependency Injection, I see that I should probably have started with gen_variables.php and turned it into a class. Of course, every inch of the whole site uses that stuff, which means I'd have to rewrite most of my code. hmmmm....
Was This Post Helpful? 0
  • +
  • -

#4 DauntlessRob   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 22-September 13

Re: Accessing Variables outside of a Class

Posted 06 June 2014 - 09:35 AM

Wow, this has been quite the trip learning how to use a class in my programming and changing everything over. It's worked pretty well and I can TOTALLY see the benefit of it now.

However, I'm in a new bind. I created a class called player. It pulls in the pdo object so that I can use sql statements to grab all the info for the current player. But I'd like this class to pull in different players by their id. So ID is a separate variable. Here's what i have so far:

class player {

	public function __construct($db) {
		global $id;
		$this->id = $id;
		$this->db = $db;
		try {
			// Grab the primary user data and location.
			$stmt = $db->prepare('SELECT * FROM Userbase WHERE id = :id');
			$stmt->execute(array(':id' => $id));
			while($rows = $stmt->fetch(PDO::FETCH_ASSOC)) {
				$this->name = $rows['name'];
				$this->date = $rows['turndate'];
	// ...
		



This of course, is just the opening of the class. $id is named first thing from a session variable. And that's where I have a problem. I'd like to be able to create a new player object and apply a new id to it so it pulls all the information for that player, and then i can make them battler or trade or whatnot.

How do I do that?
Was This Post Helpful? 0
  • +
  • -

#5 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2760
  • View blog
  • Posts: 8,062
  • Joined: 15-January 14

Re: Accessing Variables outside of a Class

Posted 06 June 2014 - 09:37 AM

Don't use a global $id, pass it as another variable to the constructor. You should avoid globals in general, especially with classes.
Was This Post Helpful? 1
  • +
  • -

#6 DauntlessRob   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 22-September 13

Re: Accessing Variables outside of a Class

Posted 06 June 2014 - 09:40 AM

Ok, wow. Why didn't that work for me when I tried it last time? Strange.

Thank you. I think I need to go get some sleep...
Was This Post Helpful? 0
  • +
  • -

#7 DauntlessRob   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 22-September 13

Re: Accessing Variables outside of a Class

Posted 06 June 2014 - 10:20 AM

alright, i want it to write to the sql table on destruct as well, but it won't let me pass the $db object to destruct so I can do so. Suggestions?

EDIT- ALright, now it's working. N/m. I'm goign to bed.

This post has been edited by DauntlessRob: 06 June 2014 - 10:22 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1