Php 致命错误:对非对象调用成员函数addSnippet()

Php 致命错误:对非对象调用成员函数addSnippet(),php,oop,object,xdebug,templating,Php,Oop,Object,Xdebug,Templating,我正在制作一个小PHP函数,它将显示我数据库中所有管理员的化身。由于某些原因,我在尝试调用函数$backend->addSnippet('login:show admins')时出现以下错误。下面是PHP类 <?php class zBackend { private $adminCount; final public function fetchAdminInfo() { global $zip, $db, $tpl; $query = $db->prepa

我正在制作一个小PHP函数,它将显示我数据库中所有管理员的化身。由于某些原因,我在尝试调用函数
$backend->addSnippet('login:show admins')时出现以下错误。下面是PHP类

<?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>
    {代码片段:显示管理员}
这里是我声明$backend的地方

<?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();
?>


如果我将代码放入文件中,它就可以正常工作,但这限制了我所能做的。我希望能够在类中完成它,并使用函数调用它。有什么想法吗?

$backend
在构造函数启动时未定义。从您发布的代码中不清楚您的
\u构造正在构造什么类,但我猜它在zTpl中。考虑将你的片段定义调用移动到一个单独的方法中,一旦所有依赖对象都被构建,你就可以调用它。
在zTpl类中:

final public function __construct() {
    global $zip;  //note that we don't need $core or 
                  //$backend, since they aren't yet defined
                  //Personally, I would pass the $zip array
                  //as a parameter to this constructor.

    $this->Define('site: title', $zip['Site']['Title']);
    //...
}

public function defineShowAdminsSnippet($backend) {
    $this->Define('snippet: show-admins', $backend->addSnippet('login: show-admins'));
}
定义对象的位置:

$tpl = new zTpl();
$backend = new zBackend();
$core = new zCore();
//new:
$tpl->defineShowAdminsSnippet();

在我看来,如果不使用
global
关键字,就更容易避免类似的依赖性问题。

知道我为什么会出现这个错误吗?我尝试过这个,但出于某种原因,代码不会在我想要的地方出现回显。它会出现在页面的顶部。它也没有显示PDO结果@Everett Green