Recently Updated

My Services

Zen Cart v1.5.3: Auto-loaded Observers!

Aug312014
One of the features quietly introduced in Zen Cart v1.5.3 is auto-loaded observers.  If you're developing a plugin that uses an observer-class, you normally have to provide two files in your plugin's distribution to get that class loaded and instantiated:

  1. /includes/auto_loaders/config.your_plugin.php

  2. /includes/classes/observers/class.your_plugin.php


Starting with Zen Cart v1.5.3, built-in functionality will do the "heavy lifting" to get your class-file loaded and instantiated -- so long as your class doesn't have any special requirements on its load point (auto-loaded classes are loaded at point 175, after all other system dependencies are loaded).   Here are the requirements (as pulled from the file /includes/init_includes/init_observers.php):

  1. The file is in the /includes/classes/observers folder and named auto.your_plugin.php.  All files in this directory that start with auto. will be included (i.e. loaded).

  2. The file defines a class named zcObserver + the CamelCased filename, e.g. a file named auto.your_plugin.php will contain a class named zcObserverYourPlugin.  A myDEBUG*.log file will be generated if a properly-named file is loaded, but the class name doesn't conform to these rules.


Here's an example that displays a message in the header, but only on the index (main) page; the file is /includes/classes/observers/auto.test_observer.php:
<?php
// -----
// An example of an auto-loaded observer, based on a feature introduced in Zen Cart v1.5.3.
//
// Displays a header-message *only* on the index page.
//
class zcObserverTestObserver extends base {

function __construct() {
$this->attach($this, array('NOTIFY_HEADER_START_INDEX'));

}

function update (&$class, $eventID, $paramsArray = array()) {
global $messageStack;
$messageStack->add ('header', 'The auto-loaded observer is loaded!', 'success');

}
}

Happy auto-loading!
Copyright © 2012-2019 Vinos de Frutas Tropicales. Powered by Zen Cart