2013-08-26 2 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에서

: 당신이 global 키워드의 사용을 제거하면 제 생각에는

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

,이 같은 의존성 문제를 방지하기 쉽다 : 당신이 당신의 객체를 정의

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')); 
} 

.

+0

나는 이것을 시험해 보았지만, 어떤 이유로 코드가 원했던 곳으로 되돌아 가지 않습니다. 페이지 상단으로 이동합니다. 또한 PDO 결과도 @Everett Green – Criesval

관련 문제