我有一个班级
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');
}
}
}
如何在课堂外访问这些变量及其内容?
在我的PHP页面中我有
$theme = new Themes();
我尝试使用
访问我的变量 $theme->them_name
但是我收到了一个未定义的错误
但是我真的不知道如何访问变量......
答案 0 :(得分:3)
根据您当前的设置,您只需拨打电话:
$theme->theme_name;
$theme->theme_by;
etc
然而,将实例变量设为公开通常是不好的做法,而是将它们设为私有并生成mutator methods。
一个例子是:
private $theme_name;
public function getThemeName(){
return $this->theme_name;
}
public function setThemeName($theme){
$this->theme_name = $theme;
}
答案 1 :(得分:1)
$this->theme_name = $result['theme_name'];
$this->theme_by = $result['theme_by'];
关于$this
前置的注释。
之后,您可以使用$theme->theme_name
等