Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 508889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:57:14+00:00 2026-05-13T06:57:14+00:00

Hello I am just learning more about using classes in PHP. I know the

  • 0

Hello I am just learning more about using classes in PHP. I know the code below is crap but I need help.

Can someone just let me know if I am going in the right direction.

My goal is to have this class included into a user profile page, when a new profile object is created, I would like for it to retrieve all the profile data from mysql, then I would like to be able to display any item on the page by just using something like this

$profile = New Profile;
echo $profile->user_name;

Here is my code so far, please tell me what is wrong so far or if I am going in the right direction?

Also instead of using echo $profile->user_name; for the 50+ profile mysql fileds, sometimes I need to do stuff with the data, for example the join date and birthdate have other code that must run to convert them, also if a record is empty then I would like to show an alternative value, so with that knowlege, should I be using methods? Like 50+ different methods?

<?PHP
//Profile.class.php file

class Profile
{
    //set some profile variables
    public $userid;
    public $pic_url;
    public $location_lat;
    public $location_long;
    public $user_name;
    public $f_name;
    public $l_name;
    public $country;
    public $usa_state;
    public $other_state;
    public $zip_code;
    public $city;
    public $gender;
    public $birth_date;
    public $date_create;
    public $date_last_visit;
    public $user_role;
    public $photo_url;
    public $user_status;
    public $friend_count;
    public $comment_count;
    public $forum_post_count;
    public $referral_count;
    public $referral_count_total;
    public $setting_public_profile;
    public $setting_online;
    public $profile_purpose;
    public $profile_height;
    public $profile_body_type;
    public $profile_ethnicity;
    public $profile_occupation;
    public $profile_marital_status;
    public $profile_sex_orientation;
    public $profile_home_town;
    public $profile_religion;
    public $profile_smoker;
    public $profile_drinker;
    public $profile_kids;
    public $profile_education;
    public $profile_income;
    public $profile_headline;
    public $profile_about_me;
    public $profile_like_to_meet;
    public $profile_interest;
    public $profile_music;
    public $profile_television;
    public $profile_books;
    public $profile_heroes;
    public $profile_here_for;
    public $profile_counter;

    function __construct($session)
    {
    // coming soon
    }

    //get profile data
    function getProfile_info(){
        $sql = "SELECT user_name,f_name,l_name,country,usa_state,other_state,zip_code,city,gender,birth_date,date_created,date_last_visit,   
        user_role,photo_url,user_status,friend_count,comment_count,forum_post_count,referral_count,referral_count_total,
        setting_public_profile,setting_online,profile_purpose,profile_height,profile_body_type,profile_ethnicity,
        profile_occupation,profile_marital_status,profile_sex_orientation,profile_home_town,profile_religion,
        profile_smoker,profile_drinker,profile_kids,profile_education,profile_income,profile_headline,profile_about_me,
        profile_like_to_meet,profile_interest,profile_music,profile_television,profile_books,profile_heroes,profile_here_for,profile_counter
        FROM users WHERE user_id=$profileid AND user_role > 0";
        $result_profile = Database::executequery($sql);

        if ($profile = mysql_fetch_assoc($result_profile)) {
            //result is found so set some variables
            $this->user_name = $profile['user_name'];
            $this->f_name = $profile['f_name'];
            $this->l_name = $profile['l_name'];
            $this->country = $profile['country'];
            $this->usa_state = $profile['usa_state'];
            $this->other_state = $profile['other_state'];
            $this->zip_code = $profile['zip_code'];
            $this->city = $profile['city'];
            $this->gender = $profile['gender'];
            $this->birth_date = $profile['birth_date'];
            $this->date_created = $profile['date_created'];
            $this->date_last_visit = $profile['date_last_visit'];
            $this->user_role = $profile['user_role'];
            $this->photo_url = $profile['photo_url'];
            $this->user_status = $profile['user_status'];
            $this->friend_count = $profile['friend_count'];
            $this->comment_count = $profile['comment_count'];
            $this->forum_post_count = $profile['forum_post_count'];
            $this->referral_count = $profile['referral_count'];
            $this->referral_count_total = $profile['referral_count_total'];
            $this->setting_public_profile = $profile['setting_public_profile'];
            $this->setting_online = $profile['setting_online'];
            $this->profile_purpose = $profile['profile_purpose'];
            $this->profile_height = $profile['profile_height'];
            $this->profile_body_type = $profile['profile_body_type'];
            $this->profile_ethnicity = $profile['profile_ethnicity'];
            $this->profile_occupation = $profile['profile_occupation'];
            $this->profile_marital_status = $profile['profile_marital_status'];
            $this->profile_sex_orientation = $profile['profile_sex_orientation'];
            $this->profile_home_town = $profile['profile_home_town'];
            $this->profile_religion = $profile['profile_religion'];
            $this->profile_smoker = $profile['profile_smoker'];
            $this->profile_drinker = $profile['profile_drinker'];
            $this->profile_kids = $profile['profile_kids'];
            $this->profile_education = $profile['profile_education'];
            $this->profile_income = $profile['profile_income'];
            $this->profile_headline = $profile['profile_headline'];
            $this->profile_about_me = $profile['profile_about_me'];
            $this->profile_like_to_meet = $profile['profile_like_to_meet'];
            $this->profile_interest = $profile['profile_interest'];
            $this->profile_music = $profile['profile_music'];
            $this->profile_television = $profile['profile_television'];
            $this->profile_books = $profile['profile_books'];
            $this->profile_heroes = $profile['profile_heroes'];
            $this->profile_here_for = $profile['profile_here_for'];
            $this->profile_counter = $profile['profile_counter'];
        }
    //this part is not done either...........
    return $this->pic_url;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:57:15+00:00Added an answer on May 13, 2026 at 6:57 am

    You might want to take a look at PHP’s [magic methods][1] which allow you to create a small number of methods (typically "get" and "set" methods), which you can then use to return/set a large number of private/protected variables very easily. You could then have eg the following code (abstract but hopefully you’ll get the idea):

    class Profile
    {
      private $_profile;
    
      // $_profile is set somewhere else, as per your original code
    
      public function __get($name)
      {
        if (array_key_exists($name, $this->_profile)) {
          return $this->_profile[$name];
        }
      }
    
      public function __set($name, $value)
      {
        // you would normally do some sanity checking here too
        // to make sure you're not just setting random variables
    
        $this->_profile[$name] = $value;
      }
    }
    

    As others have suggested as well, maybe looking into something like an ORM or similar (Doctrine, ActiveRecord etc) might be a worthwhile exercise, where all the above is done for you 🙂

    Edit: I should probably have mentioned how you’d access the properties after you implement the above (for completeness!)

    $profile = new Profile;
    
    // setting
    $profile->user_name = "JoeBloggs";
    
    // retrieving
    echo $profile->user_name;
    

    and these will use the magic methods defined above.
    [1]: https://www.php.net/manual/en/language.oop5.overloading.php

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 369k
  • Answers 369k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It allows a variable number of arguments of unspecified type… May 14, 2026 at 6:29 pm
  • Editorial Team
    Editorial Team added an answer Ok, I finality wrote it :) It took me a… May 14, 2026 at 6:29 pm
  • Editorial Team
    Editorial Team added an answer You should probably have a primary key on your table.… May 14, 2026 at 6:29 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.