Jump to content

class objects in PHP, can they persist in memory???


jnroche
 Share

Recommended Posts

Hi guys!just curious if PHP (4 & up) can handle classes/class objects that persist in memory even if the page was destroyed or reloaded fully??? as i understand it, classes (OOP in general) stays in memory unless explicitly (i know in c++ there is a delete built-in function) destroyed.... thanks :)

Link to comment
Share on other sites

HTTP is a stateless protocol, application state is not inherently preserved from one request to the next. Cookies were created to help alleviate that. After page execution ends, the only thing that PHP saves about the user is the session. You can save whatever you want, including classes, in the session and access it on other pages. You cannot save resources in the session, such as a database connection, or references to variables. If you save a class in the session, you must include the class definition before calling session_start so that PHP already has the class definition when it tries to rebuild it from the session.But other then the session, PHP does not save any information, in memory or otherwise, from request to request. But PHP does have support for shared memory if you have more then one request that need to use the same copy of the same class.As far as OOP goes, nothing should stay in memory when a program ends. When the program ends, all of the memory that was allocated should be freed up. Failure to do so would be a memory leak.

Link to comment
Share on other sites

...As far as OOP goes, nothing should stay in memory when a program ends. When the program ends, all of the memory that was allocated should be freed up. Failure to do so would be a memory leak.
I agree! you should make sure you delete objects you used in your program when you find no more reason to use it at any point especially when the entire program exits. memory leaks occur because objects were not freed up explicitly (in c++) from memory..as far as programming OOP for the web (using php), my question was more of getting persistent values across refreshed pages (especiall the index page - coz this is where everything in php is initialized), passing objects using either GET or POST or SESSION... and i have tried session and error occurs saying something about serializing the object first..I have yet to try serialize() function in php and see how it goes..thanks heaps :)
Link to comment
Share on other sites

Hi gurus!here is my sample code excerpt:on the index.php file: $MENUS = new config;$MENUS->dbconnect("localhost", "root", "", "customd_db");$fieldcapture = array("menuid", "txmenu", "groupid", "txgroupdesc");$menusarr = $MENUS->querydb("*", "tblmenus", '', $fieldcapture);...what is important is the following two lines$_SESSION['objMenus'] = serialize($MENUS);$test = unserialize($_SESSION['objMenus']);echo $test->dbname;if you run this code the name customd_db will show up.okay, now, when i redirect to another page (second page) and get the name of the database:hence on secondfile.php:include "class.Config.php";...$test2 = unserialize($_SESSION['objMenus']);echo $test2->dbname;the second page generates 1anything wrong with the code?

Link to comment
Share on other sites

You're going to have some problems using this, because you are trying to save resources into the session. The config class connects to a database, so it keeps a database connection resource. That resource cannot be stored in the session. If you want to use the same connection on several different pages, look into this function:http://www.php.net/manual/en/function.mysql-pconnect.phpBut you will still need to call it on every page, you can't just store a connection in the session and use it on another page.

Link to comment
Share on other sites

You're going to have some problems using this, because you are trying to save resources into the session. The config class connects to a database, so it keeps a database connection resource. That resource cannot be stored in the session. If you want to use the same connection on several different pages, look into this function:http://www.php.net/manual/en/function.mysql-pconnect.phpBut you will still need to call it on every page, you can't just store a connection in the session and use it on another page.
thanks justsomeguy!would this cause some serious security issues? i feel you can loop through objects in the database while connection is open and leave hackers scouting for real items/objects within your database? is this advisable?
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...