Solved

PHP Class Private Properties Not Holding Values

Posted on 2011-03-24
3
341 Views
Last Modified: 2012-05-11
Experts,

I have member_login.php that instantiate a class object like this:

                  $memObj = new memberAccount();
                  $memObj->setUsername($t_username);
                  $memObj->setUserpass($t_userpass);
                  $memObj->setUseremail($t_useremail);
                  echo 'I can get the user email from getUseremail: '.$memObj->getUseremail().'<br />';

Inside the class (memberAccount.inc) looks like this:

<?php
class memberAccount
{
      //Class page attributes
      private $usernameClass;
      private $userpassClass;
      private $useremailClass;

      function __construct()
      {
            echo "went through constructor<br />";
      }
      //setting and getting usernameClass
      function setUsername($t_username)
      {
            $this->usernameClass = $t_username;
      }
      function getUsername()
      {
            return $this->usernameClass;
      }
      //setting and getting userpassClass
      function setUserpass($t_userpass)
      {
            $this->userpassClass = $t_userpass;
      }
      function getUserpass()
      {
            return $this->userpassClass;
      }
      //setting and getting useremailClass
      function setUseremail($t_useremail)
      {
            $this->useremailClass = $t_useremail;
      }
      function getUseremail()
      {
            return $this->useremailClass;
      }

      public function display()
      {
            echo 'Username: '.$this->usernameClass.'<br />';
            echo 'userpass: '.$this->userpassClass.'<br />';
            echo 'Email: '.$this->useremailClass.'<br />';
      }
}
?>

It displays the properties in the class from calling the function display().

However, when I click a link in member_login.php and jump to member_accountChage.php, where I also call the function, display(), it doesn't display what those properties hold anymore.  They seem to be empty.

How can I make the class properties hold values while user goes back and forth between different files?  Or what am I doing wrong?

Thank you!
0
Comment
Question by:Kokko
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 35213007
Any data that is not defined in the file, you have to save and reload each time.  You can put the data in a file, a database, a $_SESSION variable (if sessions are being used), or explicitly pass the data to the next file.  Otherwise the data in a PHP page lasts only as long as it takes to execute the page.  By the time you see it on the screen, it's gone.
0
 

Author Closing Comment

by:Kokko
ID: 35214774
DaveBaldwin,

I'm using $_SESSION so I'll go this route to pass data.  I might have to ask more questions later on as I work on it, but for now I know class doesn't hold data between pages so I'm good to go.

Thank you very much!

Motoko
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 35216480
Good, thanks for the points.
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question