用于为我的博客显示不同视图的类。
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()
我无法弄清楚原因。任何帮助将不胜感激。
答案 0 :(得分:4)
您需要使用$this->function()
代替function()
来调用方法。
在旁注中,构造函数名称不正确。它是__contruct()
但需要__construct()
才能用作构造函数。除此之外,你的缩进是可怕的,使代码难以阅读。
答案 1 :(得分: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文件,您就不必再回头看了(取决于课程项目)