help with accessing variables inside of my class
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'];
}els开发者_运维技巧e{
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...
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;
}
$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
精彩评论