我正在做一個小小的PHP函數,它將顯示我數據庫中所有管理員的頭像。出於某種原因,當我嘗試調用函數$backend->addSnippet('login: show-admins');
時,出現以下錯誤。這是PHP類。致命錯誤:調用非對象的成員函數addSnippet()
<?php
class zBackend {
private $adminCount;
final public function fetchAdminInfo() {
global $zip, $db, $tpl;
$query = $db->prepare('SELECT first_name, last_name FROM zip__admins');
$query->execute();
$result = $query->fetchAll();
$id = 1;
foreach($result as $row) {
$tpl->define('admin: first_name-' . $id, $row['first_name']);
$tpl->define('admin: last_name-' . $id, $row['last_name']);
$id++;
}
$this->adminCount = $id;
}
final public function addSnippet($z) {
global $tpl;
if(isset($z) && !empty($z)) {
$this->fetchAdminInfo();
switch($z) {
case 'login: show-admins':
$tpl->write('<ul id="users">');
$id = 0;
while($this->adminCount > $id) {
$tpl->write('<li data-name="{admin: first_name-' . $id + 1 . '} {admin: last_name-' . $id + 1 . '}">');
$tpl->write('<div class="av-overlay"></div><img src="{site: backend}/img/avatars/nick.jpg" class="av">');
$tpl->write('<span class="av-tooltip">{admin: first_name-' . $id + 1 . '} {admin: last_name-' . $id + 1 . '}</span>');
$tpl->write('</li>');
}
break;
}
} else {
return false;
}
}
}
?>
這裏是我設置的功能:
final public function __construct() {
global $zip, $core, $backend;
$this->Define('site: title', $zip['Site']['Title']);
$this->Define('site: location', $zip['Site']['Location']);
$this->Define('site: style', $zip['Site']['Location'] . '/_zip/_templates/_frontend/' . $zip['Template']['Frontend']);
$this->Define('site: backend', $zip['Site']['Location'] . '/_zip/_templates/_backend/' . $zip['Template']['Backend']);
$this->Define('social: email', $zip['Social']['Email']);
$this->Define('social: twitter', $zip['Social']['Twitter']);
$this->Define('social: youtube', $zip['Social']['Youtube']);
$this->Define('social: facebook', $zip['Social']['Facebook']);
$this->Define('snippet: show-admins', $backend->addSnippet('login: show-admins'));
}
而這裏就是我所說的功能:
<ul id="users">
{snippet: show-admins}
<br class="clear">
</ul>
這裏就是我宣佈$後端
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', '1');
define('D', DIRECTORY_SEPARATOR);
define('Z', '_zip' . D);
define('L', '_lib' . D);
define('C', '_class'. D);
require Z . 'config.php';
require Z . L . 'common.php';
try {
$db = new PDO($zip['Database']['Data']['Source']['Name'], $zip['Database']['Username'], $zip['Database']['Password']);
} catch(PDOException $e) {
die(zipError('ZipDB: Connection Failed', $e->getMessage()));
}
require Z . C . 'class.ztpl.php';
require Z . C . 'class.zcore.php';
require Z . C . 'class.zbackend.php';
require Z . C . 'class.zmail.php';
$tpl = new zTpl();
$backend = new zBackend();
$core = new zCore();
?>
它工作得很好,如果我把代碼進入文件,但這限制了我能做的事情。我希望能夠在課堂上做到這一點,並使用函數來調用它。有任何想法嗎?
任何想法,爲什麼我得到這個錯誤? – Criesval