Jump to content

Variables in Classes Question


renj0806

Recommended Posts

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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.  :D

Link to comment
Share on other sites

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.  :D Takes up space and its not really important to be stored in a database.  :P

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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;

?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.