tsilenzio Posted July 12, 2007 Share Posted July 12, 2007 Well I want to use classes and ive used them before in Visual Basic, C++, and Java but not for PHP if I create a class what will keep it alive from page to page (because all variables die from one page load to the next) Quote Link to comment Share on other sites More sharing options...
Lumio Posted July 12, 2007 Share Posted July 12, 2007 You have to send the variables to another page. There are differente ways to save a value for another page: GET-Variables GET-Variables are in the URL... for example: www.example.com?var1=value1&var2=value2 You can get that variable like that:echo $_GET['var1']; POST-Variables POST-Variables are sent with a form. So you need a form or you send it with Javascript. You can get them with [/code]$_POST['variable-name'];[/code] COOKIE-Variables Before you can use a cookie, you must set it with for example setcookie. You can use it after a refresh ... There also other types of them... read more Quote Link to comment Share on other sites More sharing options...
tsilenzio Posted July 12, 2007 Author Share Posted July 12, 2007 yea i know how to use vairables but is there a way to send a whole class in a $_POST or $_SESSION? Quote Link to comment Share on other sites More sharing options...
trq Posted July 12, 2007 Share Posted July 12, 2007 You could use serialize, though $_SESSION vars get serialized automatically. Personally, Ive never really had the need to try and create persistant objects. Quote Link to comment Share on other sites More sharing options...
cmgmyr Posted July 12, 2007 Share Posted July 12, 2007 why can't you just re-initiate it? Quote Link to comment Share on other sites More sharing options...
per1os Posted July 12, 2007 Share Posted July 12, 2007 yea i know how to use vairables but is there a way to send a whole class in a $_POST or $_SESSION? $_SESSION would work, and no need to serialize it as thorpe said. Basically this would work: <?php include('class.def.php'); // has to be before session_Start session_start(); if (!isset($_SESSION['class_name'])) { $_SESSION['class_name'] = new class_name(); }else { $x=1; } $class &= $_SESSION['class_name']; // dunno if keeping the reference works but yea if ($x==1) echo $class->get_var('test'); $class->set_var('test', 'Testing'); $_SESSION['class_name'] = $class; ?> Quote Link to comment Share on other sites More sharing options...
tsilenzio Posted July 12, 2007 Author Share Posted July 12, 2007 well let me ask this then if i have a class asigned to each user and each class has about 8 - 20 vars in it and there will be a class for each person logged in on each page that needs the information in it (like 90% of all my pages) then when there are about 500 - 4,000 ppl on the page at once will this cause the server to laggg REALLY bad or you dunno or what? I would assume it would but then again im no PHP / MySQL expert Quote Link to comment Share on other sites More sharing options...
per1os Posted July 12, 2007 Share Posted July 12, 2007 If thats the load you except, I would re-instantiate the object each call. The session is stored in memory and could cause some potential problems. Just generate a function in the class called getUserData or PopulateClass that is called when the constructor is ran and fills the class with needed data. EDIT:: You may be fine keeping it in session like I described, but I have no clue. I never implemented that on a huge system with a lot of people. Quote Link to comment Share on other sites More sharing options...
tsilenzio Posted July 12, 2007 Author Share Posted July 12, 2007 ok good idea but would it slow up server load laot if it initalized the data for every var in each class from the tables in MySQL? or not really I know that ODBC Databases r slow on my PC so i assumed evvery database is slow like that =/ not sure if mySQL is like lightning fast compared to it.. Quote Link to comment Share on other sites More sharing options...
per1os Posted July 12, 2007 Share Posted July 12, 2007 Most servers database are really fast, you should really be only running 1 query to get all the data. Multiple queries, where 1 would suffice is what drags ya down. But MySQL is very efficient and fast if configured properly and in 3NF form. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.