2013-08-26 106 views
0

我正在做一個小小的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(); 
?> 

它工作得很好,如果我把代碼進入文件,但這限制了我能做的事情。我希望能夠在課堂上做到這一點,並使用函數來調用它。有任何想法嗎?

+0

任何想法,爲什麼我得到這個錯誤? – Criesval

回答

1

$backend在構造函數觸發時未定義。從您發佈的代碼__construct正在構建的代碼尚不清楚,但我猜測它在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關鍵字。

+0

我試過這個,但由於某種原因,代碼不會迴應我想要的地方。它進入頁面的頂部。它也沒有顯示@Everett Green的PDO結果 – Criesval

相關問題