1

Hmmm, so how is this done?

I have a class

class Themes extends Access
{
    public $theme_name;
    public $theme_by;
    public $theme_by_email;
    public $theme_by_website;
    public $theme_description;
    public $theme_thumb;
    public $theme_source;
    public $theme_uploaded_on;

    public function __construct()
    {
        parent::__construct();
        //$this->get_theme();
    }

    public function get_theme()
    {
        $sql = "SELECT *
                FROM `user_themes`
                WHERE `user_id` = " . $this->session->get('user_id');
        if($this->db->row_count($sql))
        {
            $result            = $this->db->fetch_row_assoc($sql);
            $this->$theme_name        = $result['theme_name'];
            $theme_by          = $result['theme_by'];
            $theme_by_email    = $result['theme_by_email'];
            $theme_by_website  = $result['theme_by_website'];
            $theme_description = $result['theme_description'];
            $theme_source      = $result['theme_source'];
            $theme_uploaded_on = $result['theme_uploaded_on'];
        }else{
            die('no results');
        }
    }
}

How can I access these variables and their contents outside of the class?

in my PHP page I have

$theme = new Themes();

I tried to access my variable using

$theme->them_name but I get an undefined error

but don't really know how I can access the variable...

2 Answers 2

3

With your current setup, all you have to do is call:

$theme->theme_name;
$theme->theme_by;
etc

However it is generally not good practice to make instance variables public, rather make them private and make mutator methods.

An example would be:

private $theme_name;

public function getThemeName(){
  return $this->theme_name;
}

public function setThemeName($theme){
  $this->theme_name = $theme;
}
Sign up to request clarification or add additional context in comments.

5 Comments

I thought about that, but I didn't want to have to make 7 calls to the database just to get the info i needed
You don't have to. You can call $theme->get_theme(), which will be your only DB call. After that, use the mutator methods. Some added benefit for using something like setThemeName, you can add optional validation before setting that value.
um, a little confused, where is $theme being called from?
For example. $theme = new Theme(); $theme->setTheme("random theme name");
oh, well in this case the theme is already in the database, so the get_theme() is calling out all the data and placing them in variables so in my application i can reference the data. like <p>Theme by: $theme-theme_by</p>
1
$this->theme_name        = $result['theme_name'];
$this->theme_by          = $result['theme_by'];

Note on $this prepended.

After that you can access the data using $theme->theme_name etc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.