Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6606607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:24:50+00:00 2026-05-25T19:24:50+00:00

i am creating a oop system in php and would like to implement more

  • 0

i am creating a oop system in php and would like to implement more observer patterns into it as i have heavy coupling between my classes that i wish to reduce.

my question is this.
in relation to best practice in design for this pattern is it ok for one class to add an observer to another class, that class is working with.
or should i keep the observer adding to the top most level of the chain?

example: (assume other methods called, but not included in the class, exist but are not important for this example.)

class orderItem extends observable {
     public function pick($qty, $user){
          $this->setUser($user);
          $position = new position($this->getPositionID());
          $position->addObserver(new ProductObserver());  // is this the best option ? ?
          $position->setQty($position->getQty() - $qty);
          $position->save();
          $this->notify(self::EVENT_PICK); // notify observers
     }
}

class orderProductObserver implements observer {
     public function update($orderitem){
           $position = new position($orderitem->getPositionID());
           $product = new product($position->getProductID());
           if($product->getQty() < $product->getMinimum()) {
                $alert = new minProductAlert($product);
           }
     }
}

class ProductObserver implements observer {
     public function update($position){
           $product = new product($position->getProductID());
           if($product->getQty() < $product->getMinimum()) {
                $alert = new minProductAlert($product);
           }
     }
}

$order = new orderItem(123);
$order->addObserver(new orderProductObserver()); // or is this the best option ??
$order->pick(2, 'bill');

Or alternatively if both methods are wrong i am very interested in your input.

would this example be the most ideal by removing dependency between orderitem and position ?

 class OrderItem extends Observable {
         public function pick($qty, $user){
              $this->setUser($user);
              $this->setPickedQty($qty);
              $this->save();
              $this->notify(self::EVENT_PICK); // notify observers
         }
    }

    class OrderItemPickObserver implements Observer {
         public function update($orderitem){
               $position = new Position($orderitem->getPositionID());
               $position->addObserver(new ProductPositionObserver());
               $position->setQty($position->getQty() - $orderItem->getPickedQty());
               $position->save();
         }
    }

    class ProductPositionObserver implements Observer {
         public function update($position){
               $product = new product($position->getProductID());
               if($product->getQty() < $product->getMinimum()) {
                    $alert = new minProductAlert($product);
               }
         }
    }
    $pickQty = 2;
    $orderitem = new OrderItem(123);
    $position = new Position($orderitem->getPositionID());
    if($position->getQty() >= $pickQty)
    {
           $orderitem->addObserver(new OrderItemPickObserver()); // or is this the best option ??
           $orderitem->pick($pickQty, 'bill');
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:24:51+00:00Added an answer on May 25, 2026 at 7:24 pm

    The second example looks good, but I’m not sure if creating new Position object inside update method of OrderItemPickObserver class. Instead, what I would suggest is to keep a Position object as a property of OrderItem class so that you can set it from outside.

    class OrderItem extends Observable {
             private $_position;
             public function setPosition($position){
                  $this->_position = $position;
             }  
             public function getPosition(){
                  return $this->_position;
             }  
        }
    

    Then update OrderItemPickObserver class:

    class OrderItemPickObserver implements Observer {
             public function update($orderitem){
                   $position = $orderitem->getPosition());
                   $position->setQty($position->getQty() - $orderItem->getPickedQty());
                   $position->save();
             }
        }
    

    And your calling code:

    $orderitem = new OrderItem(123);    
    $position = new Position();
    $position->addObserver(new ProductPositionObserver());
    $orderitem->setPosition($position);
    

    This way you can decouple OrderItemPickObserver and Position classes.

    EDITED:

    If your business logic doesn’t allow you to have a Position object in OrderItem class, you can move the same to OrderItemPickObserver since this is the class which actually uses the Position object.

    class OrderItemPickObserver implements Observer {
                 private $_position;
                 function __construct($position){
                      $this->_position = $position;
                 }  
    
                 public function update($orderitem){
                       $position = $this->_position;
                       $position->setId($orderitem->getPositionID());
                       $position->setQty($position->getQty() - $orderItem->getPickedQty());
                       $position->save();
                 }
            }
    

    And your calling code:

    $orderitem = new OrderItem(123);    
    $position = new Position();
    $position->addObserver(new ProductPositionObserver());
    ...
    ...
    $orderitem->addObserver(new OrderItemPickObserver($position)); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

last week I have spend on creating a dynamic DB-to-OOP mapping engine in PHP.
I am learning OOP with PHP. I am creating a class to extract XML
OOP usually requires instantiation (creating an instance of class before using) like this: var
I have a problem grasping the OOP concept when it comes to creating objects
I am just getting into OOP and framework design. I have started by using
I've built my own educational MVC framework to learn more about PHP OOP, which
I have decided to start coding using OOP and a PHP framework. I have
I have been creating a php application that makes quite a few queries to
I have managed to implement OOP of Cart Basket An Item contain 1 or
Creating hashes of hashes in Ruby allows for convenient two (or more) dimensional lookups.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.