Zen Cart Logo
Forums / Code Collaboration / Accessing public class variables in observer class

Accessing public class variables in observer class

Views: 6,655

Results 1 to 2 of 2
20 Mar 2019, 5:33 PM
#1
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Accessing public class variables in observer class

I'm trying to write an observer for a notifier in includes/classes/order.php. The following code appears in order.php:

    // Issue a notification, allowing an observer to potentially make changes to any of the
    // order-related addresses and/or the country/zone information used to determine the
    // order's products' tax rate.
    //
    $this->notify('NOTIFY_ORDER_CART_AFTER_ADDRESSES_SET', '', $taxCountryId, $taxZoneId);

So how do I access order-related addresses, since they are not included in the notifier parameters? What is the proper OOP way to access the order class public variables $customer, $delivery, and $billing in the observer class? I tried $order_data = new order(); in the observer but got a fatal memory exceeded error. Must I use $GLOBALS?

Frustrated as usual with OOP.:(
Dave

20 Mar 2019, 6:11 PM
#2
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Accessing public class variables in observer class

Your observer method can be written two ways. The method incorporated in ZC 1.5.3 and above uses camelcase to search for a function within the observer class appended to the word update. The method used since development of the notifier/observer classes has been to use the function update.

So for your observation of:
$this->notify('NOTIFY_ORDER_CART_AFTER_ADDRESSES_SET', '', $taxCountryId, $taxZoneId);

You could use:

<?php
class my_order_class extends base
{
  function __construct() {
   $this->attach($this, array('NOTIFY_ORDER_CART_AFTER_ADDRESSES_SET'));
  }
 function updateNotifyOrderCartAfterAddresses(&$callingClass, $notifier, $empty_string, &$taxCountryId, &$taxZoneId) {
   // do action on data.
   // to access variables of the calling class, then can use:
   // $callingClass->billing, $callingClass->shipping, etc...
 }
}

If want to use the update method alone, then need to determine or make clear about how the code will be used as a similar number of parameters will be needed in the update method, but if the code is used in ZC 1.5.1 or below and a default value is not applied, then an error is expected to occur because ZC 1.5.1 and below only pass information for the first three parameters.

The parameters, whatever variable name you want to use, relate to: 1) the class that made notification to be observed, 2) the notifier that was used to call the function, and the third 3) is an empty array if no data is provided. In this case an empty string is provided.

Further, the first variable is passed as mutable and the addition of & identifies that the data should be able to be modified and would be seen back in the calling class. The fourth through 11th parameters are also mutable by design. The second and third are immutable, even if an ampersand were added.