Solved

PHP Class Private Properties Not Holding Values

Posted on 2011-03-24
3
328 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

867 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now