使用函数加载数据库值

时间:2016-12-24 12:42:53

标签: php

我没有看到这样的话题,所以我发布了一个新主题 首先,我很抱歉我的英语不好。

我想要一个可以在全局变量中存储来自数据库的值的公共函数,并且使用该值,php脚本选择网站模板。你能救我吗?

    private $page_title = null;
private $body_title = null;
private $body_content = null;
private $template = null;
private $settings = array();
private $file_template = 'content';
private $path = 'templates/';
private $parse_page = false;
global $template2;

public function GetHASH()
{
    return $this->hash;
}

function Tema()
{
    global $db,$user_class;
    if($user_class->authorized)
    {
        $row = $db->sql("SELECT * FROM `ucp_users` WHERE `user_hash` = '".$this->GetHASH()."'");
        $array = $db->get_array($row);
        $template2 = $array['template'];
    }
    else
    {
        $template2 = '1';
    }
    return $template2;
}
function __construct() { 
    switch($template2) {
        default:
        {
            $template_name = 'dsa/';
            $template2 = 1;
        }
        break;
        case 0: $template_name = 'lsrp/';
        break;
        case 1: $template_name = 'dsa/';
        break;
    }
    $this->path = $this->path.''.$template_name;
}

谢谢。

1 个答案:

答案 0 :(得分:0)

实例化类时,会调用构造函数,此时尚未调用tema方法,因为没有实例就无法调用它。一种解决方案是在构造函数中调用tema方法,如下所示:

function __construct() {
  $template2 = this->tema(); 
  switch($template2) {
    default:
    {
        $template_name = 'dsa/';
        $template2 = 1;
    }
    break;
    case 0: $template_name = 'lsrp/';
    break;
    case 1: $template_name = 'dsa/';
    break;
  }
  $this->path = $this->path.''.$template_name;

}