Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jan 2007
    Posts
    217
    Thanks
    9
    Thanked 0 Times in 0 Posts

    More Classes Help

    PHP Code:
    class User{

     var 
    $username2;
     var 
    $activepet;
     var 
    $query;
     var 
    $spare;
     
     function 
    User() {
       
    $this->query=mysql_query("SELECT * FROM `Member` WHERE `loginName` = '$this->username2'") or die("Error with User Function" mysql_error());
       
    $this->spare=mysql_fetch_array($this->query);
       
    $username2 $this->spare['loginName'];
       
    $activepet $this->spare['activepet'];
       return 
    $username2;
       return 
    $activepet;
       }

    So this is my class, and what happens is this:

    PHP Code:
    $details = new User;
    $username = ($_SESSION['username']);
    $details->username2=$username;
    $details->User(); 
    I was wondering how i get both $username2 and $activepet results back? Simple echoing the vairables doesn't work =/

  • #2
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    Thanked 30 Times in 29 Posts
    You should create functions (sometimes referred to as getters) to get values back from within a class.

    try the adding the following functions inside your class

    Code:
    function getUserName() {
    	   return $this->username2;
       }
       function getActivePet() {
    	   return $this->activepet;
       }
    Once you have done that, try and print the values like so
    Code:
    echo "User Name: " . $details ->getUserName();
    echo "<br/>Active pet: " . $details ->getActivePet();
    I would recommend that you look at the OOP tutorial from php.net. here is the url http://www.php.net/oop

    cheers,
    Ess


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •