Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP Class Private Properties Not Holding Values

Posted on 2011-03-24
3
Medium Priority
?
349 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 84

Accepted Solution

by:
Dave Baldwin earned 2000 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 84

Expert Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

604 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