renj0806 Posted February 15, 2007 Share Posted February 15, 2007 How can i make a variable in my class not change its value after it is declared. Take for example the code below: Datamanager.php $datamanager = new DataManager(); Class DataManager { var $index = 0; function DataManager(){ } function incrementIndex(){ $this->index++; } function getIndex(){ return $this->index; } } In my first php page i include("Datamanager.php"), everything works fine if i do things in one page. If i incrementIndex() 5 times then the value of getIndex() returns 5. However, i face a problem when i do it in another page where i include("Datamanager.php") again because an error occurs when i dont include it. When i getIndex() in a second php page after doing an incrementIndex() 5x on the other page, the value returned by getIndex() is back to 0. Can anyone explain to me why? Or can anyone give me a sample wherein the returned value of getIndex() will not be back to 0? Please...Thanks! Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 15, 2007 Share Posted February 15, 2007 I'll try to explain to you why that happens in something of an example. Think of a class declaration as a recipe. Now, when you include the page you're baking a cake following the recipe. (Creating an object via new ClassName()). Every time you go to a page a new cake is baked, this is because the cake (Created Object)from the last page isn't get passed over. Objects (cakes) are garbaged after that page is done, unless you store them somewhere. Does that make sense, or am I just talking like a crazy person? Quote Link to comment Share on other sites More sharing options...
renj0806 Posted February 15, 2007 Author Share Posted February 15, 2007 That made sense. And i also knew that was the reason. But im still new at PHP and i cant find any other way to improvise my current codes. I just dont know a solution thats why Im bringing it up in this forums for some experts like you to suggest something. Thanks for you analogy btw. Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 15, 2007 Share Posted February 15, 2007 Things you can do are to save the object in the session, which I'm against, but maybe that's just me. You could also save out the necessary variables to a cookie, a session, or a database, and retrieve it on the next page. Quote Link to comment Share on other sites More sharing options...
renj0806 Posted February 15, 2007 Author Share Posted February 15, 2007 A session, im not into it also. A cookie, that i dont know. Coz i also have a multidimensional array within my class so i think those things wont work for it, right? As for database, its not worth it. Takes up space and its not really important to be stored in a database. Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 15, 2007 Share Posted February 15, 2007 Actually, from what I've heard cookies play really nicely with PHP arrays, though I've never done it myself. Quote Link to comment Share on other sites More sharing options...
renj0806 Posted February 15, 2007 Author Share Posted February 15, 2007 Oh I see. Im gonna look into cookies. But for anyone there who knows how to handle such arrays in cookies. Please do post an example. It would really help. Thanks for you help Balmung-San. Im gonna sleep now and do things again tomorrow. Im hoping some examples will be posted. See ya! Quote Link to comment Share on other sites More sharing options...
renj0806 Posted February 16, 2007 Author Share Posted February 16, 2007 Anyone have an example how to incorporate an array into cookies? Please do post an example please. Thanks! And another question, can cookies solve my problems Ive said above? Quote Link to comment Share on other sites More sharing options...
trq Posted February 16, 2007 Share Posted February 16, 2007 Easiest way would be to serialize it first but honestly, unless you need to keep this object persistent for long periods of time, sessions are the way to go. They automatically serialize the data for you and are simple to use. Quote Link to comment Share on other sites More sharing options...
renj0806 Posted February 16, 2007 Author Share Posted February 16, 2007 Ok, so im gonna use sessions. And arrays can be stored in sessions right? How can arrays be retreived in sessions? $_SESSION['session_name'][1] ? Do I have the right syntax? Quote Link to comment Share on other sites More sharing options...
trq Posted February 16, 2007 Share Posted February 16, 2007 I thought you wanted to store objects? Quote Link to comment Share on other sites More sharing options...
trq Posted February 16, 2007 Share Posted February 16, 2007 A simple example. 3 pages. foo.class.php <?php class foo { public $a = 1; function inca() { $this->a++ } ?> p1.php <?php session_start(); include 'foo.class.php'; $f = new foo(); echo $f->a; $f->inca(); $_SESSION['foo'] = $f; echo "<a href='p2.php'>next</a>"; ?> p2.php <?php session_start(); include 'foo.class.php'; $f = $_SESSION['foo'] echo $f->a; $f->inca(); echo $f->a; ?> Quote Link to comment Share on other sites More sharing options...
renj0806 Posted February 16, 2007 Author Share Posted February 16, 2007 For example if my object was an array type, how would i do that? Quote Link to comment Share on other sites More sharing options...
renj0806 Posted February 16, 2007 Author Share Posted February 16, 2007 A simple example. 3 pages. foo.class.php <?php class foo { public $a = 1; function inca() { $this->a++ } ?> p1.php <?php session_start(); include 'foo.class.php'; $f = new foo(); echo $f->a; $f->inca(); $_SESSION['foo'] = $f; echo "<a href='p2.php'>next</a>"; ?> p2.php <?php session_start(); include 'foo.class.php'; $f = $_SESSION['foo'] echo $f->a; $f->inca(); echo $f->a; ?> what if i set $a as an array variable? foo.class.php <?php class foo { public $a = array("Hello","World"); function conca() { $this->a[] = a[] . "Everyone" } ?> How can I echo $a after putting it in a $_SESSION['foo'] just like in p1.php i quoted above? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 16, 2007 Share Posted February 16, 2007 $foo = $_SESSION['foo']; print_r($foo->a); Just like you normally would 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.