SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2004
    Location
    Fort Lauderdale
    Posts
    522
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Design of a class.

    I use php4. I have a question about filling out data for each variable in my User class.

    Code:
    <?php
    
    class User{
    
    var $userid;
    var $username;
    var $firstname;
    var $lastname;
    var $password;
    var $email;
    var $signupdate;
    var $lastlogin;
    var $city;
    var $state;
    var $zip;
    var $registrationstatus;
    var $activestatus;
    var $forumname;
    
    function User($userId){
    	$this->userid=$userId;
    }
    
    function getFirstName(){
    	return $this->firstname;
    }
    
    }//end class User
    
    //**********************************************
    
    class Trainer extends User{
    
    var $experience;
    var $trainerstyle;
    var $reasonfortraining;
    var $fieldofstudy;
    var $degree;
    var $yearsofexperience;
    var $certifications;
    var $educationalbackground;
    var $specialization;
    var $liabilityinsurance;
    var $cpr;
    
    function Trainer($id){
    	$this->User($id);//passing it to the parent constructor
    }
    
    }//end Trainer class
    ?>
    how do I fill the information in those classes. do I need to create another class that will fill out those values for me?
    when I code this:
    Code:
    $Trainer = new Trainer(34);
    $Trainer->getFirstName();
    $Trainer->getExperience();
    the classes are still empty so I get nothing....I need a way to fill out all the values . Should I include a function in Trainer class that fills out the trainer values and separate User class function that upon calling fills out user values in the User class?

    Help me out...

  2. #2
    SitePoint Guru Ruben K.'s Avatar
    Join Date
    Jun 2005
    Location
    Alkmaar, The Netherlands
    Posts
    693
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When using extensions, note that the functions in the extended class overwrite the ones in the base class.

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2004
    Location
    Fort Lauderdale
    Posts
    522
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i know dat
    still didnt answer my question

  4. #4
    SitePoint Enthusiast Zeldinha's Avatar
    Join Date
    Sep 2004
    Location
    Barcelona [Spain]
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see no function to retrieve data from a database or a file or anything (and assign it to the class variables). Did you simply not include them here or didn't write them at all?

  5. #5
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    Germany
    Posts
    550
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    class UserManager
    {
        protected 
    $conn;
        
        public function 
    __construct()
        {
            
    $reg Registry::getInstance();
            
    $this->conn $reg->getDBConnection();
        }
        
        public function 
    getUserById($id)
        {
            
    $stmt $this->conn->prepareStatement('SELECT user_id, user_name, email FROM users WHERE user_id = ?');
            
    $stmt->setInt(1$id);
            
            
    $rs $stmt->executeQuery();
            
    $rs->next();
            
    $user = new User();
            
    $user->setUsername($rs->getString('user_name'));
            
    $user->setUserId($rs->getInt('user_id'));
            return 
    $user;
        }

    Note that this is php5 code, with Creole as the db-class. This will of course not work with php4 But should give you an idea. Usage woulde be sth. like
    PHP Code:
    $um = new Usermanager();

    $user $um->getUserById(32); 

    You could also add a function setProperties($array) to your user/trainer class, which takes the array from the query and assigns the properties (if the keys and properties have the same name.

    hth


Bookmarks

Posting Permissions

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