Utherr12 Posted November 3, 2010 Share Posted November 3, 2010 i'm getting this error "Undefined variable: bag_space in D:\webserver\game\lib_main.php on line 70"http://pastebin.ca/1980976Why is it giving me that error since my variable is defined :/ (and more than that, its initialized) Link to comment Share on other sites More sharing options...
boen_robot Posted November 3, 2010 Share Posted November 3, 2010 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 More sharing options...
Utherr12 Posted November 3, 2010 Author Share Posted November 3, 2010 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 More sharing options...
boen_robot Posted November 3, 2010 Share Posted November 3, 2010 Try it... I think it should, but if it doesn't, you can always trim() it. Link to comment Share on other sites More sharing options...
Utherr12 Posted November 3, 2010 Author Share Posted November 3, 2010 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 More sharing options...
thescientist Posted November 4, 2010 Share Posted November 4, 2010 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 More sharing options...
justsomeguy Posted November 4, 2010 Share Posted November 4, 2010 Close. $phpDB = mysql_connect(...);class soul{ some_method() { global $phpDB; }} Link to comment Share on other sites More sharing options...
thescientist Posted November 4, 2010 Share Posted November 4, 2010 so close... Link to comment Share on other sites More sharing options...
Utherr12 Posted November 5, 2010 Author Share Posted November 5, 2010 It says "syntax error" on dreamweaver :/ everytime i add "global" in front of "$phpDB". Link to comment Share on other sites More sharing options...
thescientist Posted November 5, 2010 Share Posted November 5, 2010 did you try it anyway? Link to comment Share on other sites More sharing options...
justsomeguy Posted November 5, 2010 Share Posted November 5, 2010 You need the word "function" before the function name. Link to comment Share on other sites More sharing options...
Utherr12 Posted November 7, 2010 Author Share Posted November 7, 2010 What function ? Link to comment Share on other sites More sharing options...
justsomeguy Posted November 7, 2010 Share Posted November 7, 2010 The only function being defined. $phpDB = mysql_connect(...);class soul{ function some_method() { global $phpDB; }} Link to comment Share on other sites More sharing options...
Utherr12 Posted November 8, 2010 Author Share Posted November 8, 2010 that's not the problem i just forgot to mention that in my posthere's how it looks in my code: http://pastebin.ca/1985135If i try to use that method it will return an error saying that $phpDB is not defined :/ Link to comment Share on other sites More sharing options...
Synook Posted November 8, 2010 Share Posted November 8, 2010 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 More sharing options...
Utherr12 Posted November 8, 2010 Author Share Posted November 8, 2010 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 More sharing options...
Synook Posted November 8, 2010 Share Posted November 8, 2010 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 More sharing options...
Utherr12 Posted November 8, 2010 Author Share Posted November 8, 2010 ok...so which is the correct variant ? putting the global keyword at declaration or every time i call it in my methods? Link to comment Share on other sites More sharing options...
Synook Posted November 9, 2010 Share Posted November 9, 2010 In the methods. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.