Difference between revisions of "Zen Cart observer"

From Wiki @ Karl Jones dot com
Jump to: navigation, search
(Created page with "A '''Zen Cart observer''' is an Observer design pattern in Zen Cart. == See also == * Observer pattern * Zen Cart * Zen Cart API")
 
Line 1: Line 1:
 
A '''Zen Cart observer''' is an [[Observer pattern|Observer design pattern]] in [[Zen Cart]].
 
A '''Zen Cart observer''' is an [[Observer pattern|Observer design pattern]] in [[Zen Cart]].
 +
 +
== Examples ==
 +
 +
=== Example one ===
 +
 +
* https://www.zen-cart.com/showthread.php?141092-Using-an-observer-to-require-login-when-accessing-a-certain-custom-page
 +
 +
=== Example two ===
 +
 +
Observer/notifier the "base" class
 +
 +
* https://www.zen-cart.com/showthread.php?62246-observer-notifier-the-quot-base-quot-class
 +
 +
QUESTION:
 +
 +
In the API turtorial it is explained that every class that want to make use of observer/notifier system has to extend the "base"
 +
class. However I cannot see how this can work. Maybe it is because I am a C++ guru and not a PHP. The problem as I see it is that we get different instances of the "base" class for every instance of observer or notifier class. Therefore the following observerclass will never get the notification from the shopping_cart class.
 +
 +
<code>
 +
class myObserver extends base {
 +
function myObserver() {
 +
$this->attach($this, array('NOTIFIER_CART_ADD_CART_END'));
 +
}
 +
...
 +
}
 +
</code>
 +
 +
If the base class was static or was working on some global variables then it would be different. What have I misunderstood?
 +
 +
REPLY:
 +
 +
The documentation is buggy.
 +
 +
Your c'tor should instead say
 +
 +
<code>$_SESSION['cart']->attach($this, array('NOTIFIER_CART_ADD_CART_END'));</code>
 +
 +
for cart stuff. The cart object maintains a list of observers and calls them when
 +
the notify() method is called for the events they have registered for. Similarly, zco_notifier, etc.
 +
  
 
== See also ==
 
== See also ==

Revision as of 11:51, 12 October 2015

A Zen Cart observer is an Observer design pattern in Zen Cart.

Examples

Example one

Example two

Observer/notifier the "base" class

QUESTION:

In the API turtorial it is explained that every class that want to make use of observer/notifier system has to extend the "base" class. However I cannot see how this can work. Maybe it is because I am a C++ guru and not a PHP. The problem as I see it is that we get different instances of the "base" class for every instance of observer or notifier class. Therefore the following observerclass will never get the notification from the shopping_cart class.

class myObserver extends base { function myObserver() { $this->attach($this, array('NOTIFIER_CART_ADD_CART_END')); } ... }

If the base class was static or was working on some global variables then it would be different. What have I misunderstood?

REPLY:

The documentation is buggy.

Your c'tor should instead say

$_SESSION['cart']->attach($this, array('NOTIFIER_CART_ADD_CART_END'));

for cart stuff. The cart object maintains a list of observers and calls them when the notify() method is called for the events they have registered for. Similarly, zco_notifier, etc.


See also