Jump to content

Undefined var lolwut


Utherr12

Recommended Posts

In PHP, you need to explicitly address the class member. Using simply $bag_space doesn't automatically fall back to the class member, as other C languages do.i.e.

class soul{	public $inventory = array();	public $bag_space = 5;			function AddItem($item)	{		if($this->bag_space==0)		{			return 0;		}		else		{			$this->inventory[] = $item;			$this->bag_space--;		}	}	}$_soul->AddItem("New Item");$_soul->AddItem("NewItem1");$item1 = $_soul->inventory[0];$item2 = $_soul->inventory[1];

Link to comment
Share on other sites

Damn...i know C++ before php and it's a bit difficult to accommodate to.LE: thanks...now it works like a charm...it's my first very attempt at OOP programming (i dont know oop in c++, i just happened to know some basics, no programming xp whatsoever)I have a quick question: if i have the string " 89" and i use the function to convert to an integer, would it safely convert? because i have a space in front of 89.

Link to comment
Share on other sites

Ok, i have another minor headache...i have this:

$phpDB = mysql_connect(...);class soul{	some_method()	{	}}

So in the "some_method" method i want to use the global variable $phpDB (so i dont have to do that for every method), but it returns an error saying that $phpDB is not defined. Isn't $phpDB a global variable that works in any class too ?

Link to comment
Share on other sites

try declaring $phpDB with the global keyword before it. i.e.

global $phpDB = mysql_connect(...);class soul{	some_method()	{	}}

Link to comment
Share on other sites

Using the global statement doesn't change anything? That may be because you are closing the connection immediately after defining the class...

Link to comment
Share on other sites

Using the global statement doesn't change anything? That may be because you are closing the connection immediately after defining the class...
Then when should i close it? I don't like keeping useless things opened >.<. I was thinking of opening the connection in my main page then...and only IF i opened it should the class methods work.So its like this: main.php <-- where i open the connection $phpDB and define $_soul = new Soul(); then use some methods from lib_main.php (where i keep all the objects) that have in the sql statements of mysql_query that $phpDB, then i could close it in main.php after i'm finished.--OR--I could skip all this and just won't close it. Would it do any harm if i don't close an mysql connection in php ?
Link to comment
Share on other sites

Connections are closed automatically when the script ends, so unless you are opening lots and lots of connections on one page it shouldn't be a problem at all. Only when using persistent connections do you really need to worry about when things are closed.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...