Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2012
    Posts
    481
    Plugin Contributions
    0

    Default 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:
    Code:
        // 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

  2. #2
    Join Date
    Jul 2012
    Posts
    16,817
    Plugin Contributions
    17

    Default 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:
    Code:
    <?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.
    ZC Installation/Maintenance Support <- Site
    Contribution for contributions welcome...

 

 

Similar Threads

  1. Using Observer/Notifier System in order class
    By yesaul in forum Code Collaboration
    Replies: 5
    Last Post: 26 Sep 2016, 07:24 PM
  2. getting SQL errors with my observer class code,
    By tcarden in forum All Other Contributions/Addons
    Replies: 1
    Last Post: 24 Apr 2013, 09:27 PM
  3. setting $_SESSION variables in an Observer Class
    By tcarden in forum Contribution-Writing Guidelines
    Replies: 1
    Last Post: 13 Feb 2013, 06:37 AM
  4. session objects in the observer class
    By wdrwc in forum All Other Contributions/Addons
    Replies: 0
    Last Post: 18 Jul 2006, 06:48 PM
  5. session objects in the observer class
    By wdrwc in forum Templates, Stylesheets, Page Layout
    Replies: 0
    Last Post: 18 Jul 2006, 11:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg