-1

类为我的博客显示不同的视图。

class SB_Display {

    public function __contruct() {
        include_once('settings/db.settings.php');

        $mysqli = new mysqli($SB_dbsettings['host'],$SB_dbsettings['user'],$SB_dbsettings['pass'],$SB_dbsettings['dbname']);
    }

    private function List_Display() {

        $VIEW = '';

        include_once('views/list.html.view.php');

        $sql = "SELECT * FROM sb_posts ORDER BY ID DESC LIMIT '$SETTINGS->maxposts'";
        $sql = $mysqli->real_escape_string($sql);
        $res = $mysqli->mysqli_query($sql);

        if($res->numrows > 0) {
        $res->data_seek(0);

            while ($row = $res->fetch_assoc()) {
                foreach($row as $key => $value) {
                $BLOG->$key = $value;
                $VIEW .= $HTML;
                }
            }
        } else {
            $VIEW .= 'No Posts To Display';
        } 

        return $VIEW;
    }


    private function Single_Display($id) {
        $VIEW = '';

        include_once('views/single.html.view.php');

        $sql = "SELECT * FROM sb_posts WHERE BID = '$id'";
        $sql = $mysqli->real_escape_string($sql);
        $res = $mysqli->mysqli_query($sql);
        $row = $res->fetch_assoc();

        foreach($row as $key => $value) {
            $BLOG->$key = $value;
        }

        $VIEW .= $HTML;

        return $VIEW;
    }


    private function Create_Display() {        
        include_once('views/create.html.view.php');
        return $HTML;
    }

    private function Edit_Display($id) {
        $VIEW = '';  

        $sql = "SELECT * FROM sb_posts WHERE BID = '$id'";
        $sql = $mysqli->real_escape_string($sql);
        $res = $mysqli->mysqli_query($sql);
        $row = $res->fetch_assoc();

        foreach($row as $key => $value) {
            $BLOG->$key = $value;
        }
        $BLOG->id = $id;
        $VIEW .= $HTML;

        return $VIEW;

    }


    public function SB_Get_Display($type,$id) {
        switch($type) {
         case 'list':
             $this->content = List_Display();
             return $this;
             break;
         case 'single':
             $this->content = Single_Display($id);
             return $this;
             break;
         case 'create':
             $this->content = Create_Display();
             return $this;
             break;
         case 'edit':
             $this->content = Edit_display($id);
             return $this;
             break;
        }
    }
}

以下列方式使用此类时..

$BODY = new SB_Display();
$BODY->SB_Get_Display('list','');

我收到此错误:

致命错误:调用未定义的函数 List_Display()

我不知道为什么。任何帮助将不胜感激。

4

2 回答 2

4

您需要使用$this->function()而不是function()调用方法。

在旁注中,您的构造函数名称不正确。它是__contruct()但需要__construct()用作构造函数。除此之外,您的缩进很糟糕,使代码难以阅读。

于 2012-05-31T21:38:49.090 回答
0

我同意@Corbin,这是一个非常糟糕的主意。另外,就个人而言,我喜欢自动加载类方法。

有人可能会说这是草率且简单的方法,但它迫使您真正考虑您的类名和目录,并且在重命名文件/类时避免出现问题。在您的情况下,您必须搜索所有试图包含它的文件并手动重命名所有内容。

“我的”方法:在站点的根目录中创建一个 inc.php 或其他内容,并将其放入其中:

PHP

//autoload classes
function __autoload($class_name){
    set_include_path(get_include_path().PATH_SEPARATOR.'/usr/share/file/'); //also     
    //include magic file location
    // put the path to your class files here
    $path = $_SERVER['DOCUMENT_ROOT'].'/lib/classes/'; 
    // tell PHP to scan the default include path AND your include path
    set_include_path(get_include_path() . PATH_SEPARATOR . $path);
    // name your classes and filenames with underscores, i.e., Net_Whois stored in 
    //Net_Whois.php
    $classfile = str_replace("_", DIRECTORY_SEPARATOR, $class_name) . ".php";
    require_once($classfile);
}

只需在 inc.php 文件中初始化您的数据库连接,您几乎可以在任何地方访问它们。

现在只需在您创建的每个新类中包含 inc.php 文件,您就不必再回头看(当然取决于项目)

于 2012-05-31T22:00:20.437 回答