Domanda

I'm making a little PHP function that will show the avatar of all the administrators in my database. For some reason, I get the following error when I try to call the function $backend->addSnippet('login: show-admins');. Here is the PHP Class.

<?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;
    }
}
}
?>

Here is where I set the function:

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

And here is where I call the function:

<ul id="users">
  {snippet: show-admins}
  <br class="clear">
</ul>

Here is where I declare $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();
?>

It works just fine if I put the code into the file, but that limits what I can do. I want to be able to do it in a class and use functions to call it. Any ideas?

È stato utile?

Soluzione

$backend isn't defined when your constructor fires. It's unclear from the code you've posted what class your __construct is constructing, but I'm guessing it's within zTpl. Consider moving your snippet definition call to a separate method, which you can call once all dependent objects have been constructed.

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

Where you define your objects:

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

In my opinion, it is easier to avoid dependency problems like this if you eliminate use of the global keyword.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top