An Object of Objects

Discussion in 'PHP' started by Weirfire, Jan 15, 2009.

  1. #1
    I'm looking to create an object called order creating some details relating to orders that come through a certain website.

    class order{
    
        var $id, $customer_id, $currency, $order_total;
    
    }
    PHP:
    Is it possible to create an order total class and nest the object within class order and replace $currency and $order_total

    e.g.

    class order_total{
        var $currency, $order_total;
    }
    
    class order {
         var $id, $customer_id, $order_total = new order_total;
    }
    
    PHP:
    If there is a way of doing it I would be very happy to know how?


    Thanks!
     
    Weirfire, Jan 15, 2009 IP
  2. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here's a skeleton:

    
    
    
    class Order {
    
      var $id, $customer_id, $total_obj;
    
      function Order() {
    
        $total_obj = new OrderTotal;
      }
    
      function getTotal() {
        
        return $total_obj->getTotal();
      }
    
      function getCurrency() {
        
        return $total_obj->getCurrency();
      }
    
    }
    
    
    class OrderTotal {
      
      var $currency, $total;
    
      function OrderTotal() {
        
      }
    
      function getTotal() {
        
        return $total;
      }
    
      function getCurrency() {
        
        return $currency;
      }
    }
    
    PHP:
    Currency can be its own object too.
     
    LogicFlux, Jan 15, 2009 IP
    Weirfire likes this.
  3. artiskool

    artiskool Peon

    Messages:
    34
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $total_obj is not accessing in other methods, using $this->total_obj works.

    
    class Order {
    
      var $id, $customer_id, $total_obj;
    
      function Order() {
    
        $this->total_obj = new OrderTotal;
      }
    
      function getTotal() {
        
        return $this->total_obj->getTotal();
      }
    
      function getCurrency() {
        
        return $this->total_obj->getCurrency();
      }
    
    }
    
    PHP:

     
    artiskool, Jan 15, 2009 IP
    Weirfire likes this.
  4. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yeah, you're right, I missed that.

    
    class Order {
    
      function Order() {
    
        $this->total_obj = new OrderTotal;
      }
    
      function getTotal() {
        
        return $this->total_obj->getTotal();
      }
    
      function getCurrency() {
        
        return $this->total_obj->getCurrency();
      }
    }
    
    
    class OrderTotal {
      
      var $currency, $total;
    
      function OrderTotal() {
        
      }
    
      function getTotal() {
        
        return $this->total;
      }
    
      function getCurrency() {
        
        return $this->currency;
      }
    }
    
    PHP:
    I forgot the $this in OrderTotal too.

    @the original poster : If you're using PHP5 and want to get into a bit more advanced concepts -- access specifiers -- then make $total_obj private in Order. It probably doesn't need to be accessed directly. Order can be the "gateway" to OrderTotal if the outside world needs any of its functionality, at least in this case.
     
    LogicFlux, Jan 15, 2009 IP
  5. artiskool

    artiskool Peon

    Messages:
    34
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You can also make use of the Data Hiding/Encapsulation concept in OOP :)
     
    artiskool, Jan 15, 2009 IP
  6. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #6
    That is awesome - thanks very much to both of you.

    Some of this stuff is coming back to me from my Java days at university... OOP is sooo efficient. (You just can't say those kind of things to normal people lol)
     
    Weirfire, Jan 19, 2009 IP