Jump to content

PHP Objects


MrFish

Recommended Posts

I've been reading up on php objects and I would like to use them. I'm making little browser game to test the use of PHP's OOP. Right now I have a simple login that, when successful, creates a Character object. I think it would be quicker and easier to populate an object and just call the variables instead of running to the database every time I need the user information. When you login it creates this object-

$Character = new Character($results['id']);

	class Character	{		public $id = -1;		public $name = "";						public $head = 0;		public $torso = 0;		public $legs = 0;		public $shoes = 0;		public $left = 0;		public $right = 0;						public $action1 = 0;		public $action2 = 0;		public $action3 = 0;				function __construct($id)		{			//Setup Connection			define(ROOT, "XXX");			$username = XXX			$password = XXX			$server = XXX				mysql_connect($server, $username, $password);						$from = ROOT . ".users";									//Get the data			$query = "SELECT * FROM $from WHERE id = $id";								$results = mysql_query($query);			$results = mysql_fetch_assoc($results);						//Set the variables			$name = $results['name'];						$this->id = $id;			$this->name = $name;		}	}

And i'm trying to print the name here-

echo "You are at the homepage, " . $Character->name;

I know some parts of the Character object may look redundant. I've been trying to debug it and get something to display. Still nothing though. So here are my PHP OOP questions-1. When I create an object will it be destroyed when I leave the page or site? I understand a lot about OOP and use it frequently in Java but I'm not sure how this works online.1.a. If the object is not destroyed can I use it on other pages like now?2. Will I need to create a different Character object name for each person that logs in? Or is it somehow magical and remembers my user?3. If the object is not destroyed immediately then when will they? Is it possible to fill the server with objects and crash it? (After 500 million objects anyway...)4. Why doesn't this display anything? Is it my syntax?

Link to comment
Share on other sites

are you calling the construct method on the character object?also, you are setting $id to -1 and then using that to look up record in database, which if you're using id to get a reference to an auto-incrementing primary key in that table like I think you are, then it most certainly will always be positive.

Link to comment
Share on other sites

From what I've learned from java, which may not be similar at all to php, when you construct an object it will automatically run the constructor method. In the constructor method I supply an id in the parameters and there it changes the id. Do I need to call the __construct function on its own?

Link to comment
Share on other sites

When I create an object will it be destroyed when I leave the page or site?
Objects are like any other variables, you can save it in the session but everything gets cleaned up when the script ends.
Will I need to create a different Character object name for each person that logs in?
Yes.
Is it possible to fill the server with objects and crash it?
I'm not sure what you mean by "fill the server with objects", but the normal memory limits for PHP still apply. Your script can use as much memory as you want up until the limit. If you try to allocate more memory then you're allowed you'll get a fatal error.
Why doesn't this display anything?
You need to debug the constructor, among other things you aren't selecting a database to use. You may want to check for errors from MySQL.
Link to comment
Share on other sites

Ok, thanks justsomeguy. I'm putting my objects in sessions now. And I've verified that the object is being created and I can use var_dump on it. The problem is I don't think I'm displaying it correctly.

	session_start();		var_dump($_SESSION['Username']);			echo "<br /><br />You are at the homepage, " . $_SESSION['Username']->name;

object(__PHP_Incomplete_Class)#1 (12) { ["__PHP_Incomplete_Class_Name"]=> string(9) "Character" ["id"]=> string(1) "1" ["name"]=> string(8) "Username" ["head"]=> int(0) ["torso"]=> int(0) ["legs"]=> int(0) ["shoes"]=> int(0) ["left"]=> int(0) ["right"]=> int(0) ["action1"]=> int(0) ["action2"]=> int(0) ["action3"]=> int(0) } You are at the homepage,
I've tried adding '$' before name too.
Link to comment
Share on other sites

I'm not sure you understood. Before you start the session, the class needs to be defined. If you start the session without the class being defined, when it creates the object from the session it's not going to be a Character object, it's going to be a generic object with no methods.

Link to comment
Share on other sites

Ah, ok. I still don't really understand why that would cause such a problem but it's works now. Thanks justsomeguy!

Link to comment
Share on other sites

Because if the class is not defined when PHP tries to create the object then obviously it's not going to be able to create the correct type of object. The class needs to be defined for PHP to know what it is. It's not going to magically know what the constructor of the object does, PHP only saves object properties in the session, not methods. The methods come from the class definition.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...