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
Solved

PHP Class Private Properties Not Holding Values

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

840 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