Solved

PHP Class Private Properties Not Holding Values

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

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 82

Expert Comment

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 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

20 Experts available now in Live!

Get 1:1 Help Now