当我包含这个PHP类时,代码没有被执行

时间:2012-04-30 16:10:58

标签: php oop

我很好地创建了这个课程,但我不确定它有什么问题。如果我内部没有任何内容,代码将完美运行,

class TemplateOne{

}

但是一旦我运行这段代码就会破坏,

<?php

class TemplateOne {

    //Properties
    protected $_bgColor;
    protected $_logoImagePath;
    protected $_headerText;
    protected $_leftContentHeader;
    protected $_rightContentHeader;
    protected $_leftContentBody;
    protected $_rightContentBody;
    protected $_footer;
    protected $_mediaIframe;
    protected $_mediaHeight = '';
    protected $_mediaWidth = '';

    //DB communication
    public $DB;

    //Constructor
    public function __construct(){

        //Connect database in construct and close connection in destruct

        $config = array();

        $config['host'] = 'localhost';
        $config['user'] = 'root';
        $config['pass'] = 'root';
        $config['database'] = 'fanpage_application';

        $this->DB = new DB($config);

        //init variables
        populateDataFromDataBase();
    }

    //Functions
    public function populateDataFromDataBase() {

        //Get bgcolor
        $this->DB->("SELECT backgroundimage FROM template_style_data WHERE styleid='#list_level'");
        $data = $this->DB->Get();
        foreach($data as $key => $value)
        {
            echo $value['backgroundimage'];
        }
    }

    //Getters
    public function getBgColor()
    {
        return $this->_bgColor;
    }

    public function getLogoImagePath()
    {
        return $this->_logoImagePath;
    }

    public function getHeaderText()
    {
        return $this->_headerText;
    }

    public function getLeftContentHeader()
    {
        return $this->_leftContentHeader;
    }

    public function getRightContentHeader()
    {
        return $this->_rightContentHeader;
    }

    public function getLeftContentBody()
    {
        return $this->_leftContentBody;
    }

    public function getRightContentBody()
    {
        return $this->_rightContentBody;
    }

    public function getFooter()
    {
        return $this->_footer;
    }

    public function getMediaIframe()
    {
        return $this->_mediaIframe;
    }

    //Setters

    public function setBgColor($bgColor)
    {
         $this->_bgColor = $bgColor;
    }

    public function setLogoImagePath($logoImagePath)
    {
         $this->_logoImagePath = $logoImagePath;
    }

    public function setHeaderText($headerText)
    {
         $this->_headerText = $headerText;
    }

    public function setLeftContentHeader($leftContentHeader)
    {
         $this->_leftContentHeader = $leftContentHeader;
    }

    public function setRightContentHeader($rightContentHeader)
    {
         $this->_rightContentHeader = $rightContentHeader;
    }

    public function setLeftContentBody($leftContentHeader)
    {
         $this->_leftContentBody = $leftContentHeader;
    }

    public function setRightContentBody($rightContentBody)
    {
         $this->_rightContentBody = $rightContentBody;
    }

    public function setFooter($footer)
    {
         $this->_footer = $footer;
    }

    public function setMediaIframe($mediaIframe)
    {
         $this->_mediaIframe = $mediaIframe;
    }

}

?>

4 个答案:

答案 0 :(得分:4)

您在致电$this->时失踪populateDataFromDataBase

答案 1 :(得分:3)

这是您的错误:

$this->DB->("SELECT backgroundimage FROM template_style_data WHERE styleid='#list_level'");

这是无效的语法,您需要在$this->DB之后调用方法。

答案 2 :(得分:3)

DB类来自哪里?您可能必须include正确的类定义文件(如果尚未。)。

$this->DB = new DB($config);

以下不是合法语法。您需要实际按名称调用函数。

$this->DB->("SELECT backgroundimage FROM template_style_data WHERE styleid='#list_level'");

除非您在名为populateDataFromDataBase的全局范围内有另一个要调用的函数,否则在尝试在构造函数中调用它之前,需要先添加$this->

populateDataFromDataBase();

答案 3 :(得分:1)

就我而言,只有一个可能的答案:检查您的PHP错误http://php.net/manual/en/function.error-reporting.php

至少(仅在开发中,不显示生产中的错误):

ini_set('display_errors', 1);
error_reporting(-1);