php nesting classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • christophe.gosiau@gmail.com

    #1

    php nesting classes

    Hi everybody,
    I have the following problem:
    For an application, I made 2 classes: Order and Ticket
    In the Order object I store an array with Ticket objects.
    so far so good, now I needed the Order object inside the Ticket class.
    So I stored a copy of the Order object inside the Ticket class.

    This is what we got until now:

    class Order {
    pubic tickets = array()
    }

    class Ticket {
    public Order = null
    }


    For both classes I made static methods: get_by_id()
    Now my question: What happens if i do: Ticket::get_by_ id(5);
    A Ticket object will be created. Inside this Ticket object, an Order
    object will be created with the Order::get_by_i d($this->orderid)
    method.
    This Order object will fill his $tickets array wilt the
    Ticket::get_by_ id($ticketid) method.
    Those tickets will create new Order objects...
    So there will be a loop.
    How will php handle this?
    If this causes an error, is there another solution to fix this
    problem?

    Christophe

  • Jerry Stuckle

    #2
    Re: php nesting classes

    christophe.gosi au@gmail.com wrote:
    Hi everybody,
    I have the following problem:
    For an application, I made 2 classes: Order and Ticket
    In the Order object I store an array with Ticket objects.
    so far so good, now I needed the Order object inside the Ticket class.
    So I stored a copy of the Order object inside the Ticket class.
    >
    This is what we got until now:
    >
    class Order {
    pubic tickets = array()
    }
    >
    class Ticket {
    public Order = null
    }
    >
    >
    For both classes I made static methods: get_by_id()
    Now my question: What happens if i do: Ticket::get_by_ id(5);
    A Ticket object will be created. Inside this Ticket object, an Order
    object will be created with the Order::get_by_i d($this->orderid)
    method.
    This Order object will fill his $tickets array wilt the
    Ticket::get_by_ id($ticketid) method.
    Those tickets will create new Order objects...
    So there will be a loop.
    How will php handle this?
    If this causes an error, is there another solution to fix this
    problem?
    >
    Christophe
    >
    Christophe,

    *WHY* do you need a both a list of tickets in orders and an order in ticket?

    Either an order has tickets, or a ticket has orders. But can you
    explain how both are true?

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • christophe.gosiau@gmail.com

      #3
      Re: php nesting classes

      Hi Jerry,

      My original plan was to to so store Tickets in an array in the Order
      object.
      But, a ticket needs to be printed (render to pdf). To do that, I need
      to know which name I have to print on it (object Customer) and the
      event where it is for (object Event).
      Both Customer and Event are also stored inside the Order class.
      That is the only reason why I need the Order object inside the Ticket
      class.



      On 22 apr, 04:43, Jerry Stuckle <jstuck...@attg lobal.netwrote:
      christophe.gos. ..@gmail.com wrote:
      Hi everybody,
      I have the following problem:
      For an application, I made 2 classes: Order and Ticket
      In the Order object I store an array with Ticket objects.
      so far so good, now I needed the Order object inside the Ticket class.
      So I stored a copy of the Order object inside the Ticket class.
      >
      This is what we got until now:
      >
      class Order {
      pubic tickets = array()
      }
      >
      class Ticket {
      public Order = null
      }
      >
      For both classes I made static methods: get_by_id()
      Now my question: What happens if i do: Ticket::get_by_ id(5);
      A Ticket object will be created. Inside this Ticket object, an Order
      object will be created with the Order::get_by_i d($this->orderid)
      method.
      This Order object will fill his $tickets array wilt the
      Ticket::get_by_ id($ticketid) method.
      Those tickets will create new Order objects...
      So there will be a loop.
      How will php handle this?
      If this causes an error, is there another solution to fix this
      problem?
      >
      Christophe
      >
      Christophe,
      >
      *WHY* do you need a both a list of tickets in orders and an order in ticket?
      >
      Either an order has tickets, or a ticket has orders. But can you
      explain how both are true?
      >
      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstuck...@attgl obal.net
      =============== ===

      Comment

      • Jerry Stuckle

        #4
        Re: php nesting classes

        christophe.gosi au@gmail.com wrote:
        On 22 apr, 04:43, Jerry Stuckle <jstuck...@attg lobal.netwrote:
        >christophe.gos ...@gmail.com wrote:
        >>Hi everybody,
        >>I have the following problem:
        >>For an application, I made 2 classes: Order and Ticket
        >>In the Order object I store an array with Ticket objects.
        >>so far so good, now I needed the Order object inside the Ticket class.
        >>So I stored a copy of the Order object inside the Ticket class.
        >>This is what we got until now:
        >>class Order {
        >> pubic tickets = array()
        >>}
        >>class Ticket {
        >> public Order = null
        >>}
        >>For both classes I made static methods: get_by_id()
        >>Now my question: What happens if i do: Ticket::get_by_ id(5);
        >>A Ticket object will be created. Inside this Ticket object, an Order
        >>object will be created with the Order::get_by_i d($this->orderid)
        >>method.
        >>This Order object will fill his $tickets array wilt the
        >>Ticket::get_b y_id($ticketid) method.
        >>Those tickets will create new Order objects...
        >>So there will be a loop.
        >>How will php handle this?
        >>If this causes an error, is there another solution to fix this
        >>problem?
        >>Christophe
        >Christophe,
        >>
        >*WHY* do you need a both a list of tickets in orders and an order in ticket?
        >>
        >Either an order has tickets, or a ticket has orders. But can you
        >explain how both are true?
        >>
        >--
        >============== ====
        >Remove the "x" from my email address
        >Jerry Stuckle
        >JDS Computer Training Corp.
        >jstuck...@attg lobal.net
        >============== ====
        >
        >
        Hi Jerry,
        >
        My original plan was to to so store Tickets in an array in the Order
        object.
        But, a ticket needs to be printed (render to pdf). To do that, I need
        to know which name I have to print on it (object Customer) and the
        event where it is for (object Event).
        Both Customer and Event are also stored inside the Order class.
        That is the only reason why I need the Order object inside the Ticket
        class.
        >
        >
        Top posting fixed

        OK, but right now I'm not sure what a "Ticket" is, and what an "Order"
        is. Does the order have tickets?

        One thing is clear, though - the ticket does not have an order. It uses
        information from the order, so should reference the order. But it
        should not keep a copy of the order.

        And since the Order object itself has Ticket objects, perhaps the you
        need an Order::MakeTick et method to create the Ticket object and fill in
        the information (including a reference to itself).

        Another option here would be to not keep the Order reference in the
        Ticket. Rather, since you're storing the Ticket list in the Order
        class, have an Order::PrintTic ket method. Let the Order class print its
        info and the Ticket class print it's info. This way you don't need to
        keep even a reference to the Order object in the Ticket object.

        But you should never keep a copy of something in your program. What
        happens if you change the original, for instance?

        P.S. Please don't top post. Thanks.


        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        • Toby A Inkster

          #5
          Re: php nesting classes

          Jerry Stuckle wrote:
          Either an order has tickets, or a ticket has orders. But can you
          explain how both are true?
          I often do this sort of thing. Consider a list of authors and a list of
          books that they've written:

          <?php
          class Author
          {
          private $surname = '';
          private $forename = '';
          private $books = array();

          function __construct ($surname, $forename='')
          {
          $this->surname = $surname;
          $this->forename = $forename;

          if (!strlen($this->surname))
          throw new Exception("Auth ors need a surname!");
          }

          function get_name ($reverse=FALSE )
          {
          if (!strlen($this->forename))
          return $this->surname;

          if ($reverse)
          return "{$this->surname}, {$this->forename}";
          else
          return "{$this->forename} {$this->surname}";
          }

          function print_catalogue ()
          {
          print $this->get_name() . "\n";
          foreach ($this->books as $b)
          print "\t".$b->title."\n";
          }

          function give_credit (Book $book)
          {
          $book->author = $this;
          $this->books[] = $book;
          }
          }

          class Book
          {
          public $title;
          public $author = NULL;

          function __construct ($title)
          {
          $this->title = $title;

          if (!strlen($this->title))
          throw new Exception("Book s need a title!");
          }

          function get_reference ()
          {
          if ($this->author instanceof Author)
          return $this->author->get_name().' : '.$this->title;
          else
          return $this->title;
          }
          }


          $library['emma'] = new Book('Emma');
          $library['pp'] = new Book('Pride & Prejudice');
          $library['ss'] = new Book('Sense & Sensibility');
          $library['wh'] = new Book('Wuthering Heights');
          $library['dict'] = new Book('Concise Oxford English Dictionary');

          $authors['JA'] =new Author('Austen' , 'Jane');
          $authors['JA']->give_credit($l ibrary['emma']);
          $authors['JA']->give_credit($l ibrary['pp']);
          $authors['JA']->give_credit($l ibrary['ss']);
          $authors['JA']->give_credit(ne w Book('Love & Freindship'));

          $authors['EB'] =new Author('Bronte' , 'Emily');
          $authors['EB']->give_credit($l ibrary['wh']);

          print "My library of classic literature...\n \n";
          foreach ($library as $book)
          print $book->get_reference( )."\n";
          print "\n";
          print "All known works of classic literature...\n \n";
          foreach ($authors as $author)
          $author->print_catalogu e();
          print "\n";
          ?>

          Bi-directional links between objects are quite common. All authors have
          a list of books; all books have an author. In a more advanced
          implementation, there might be a Library class too, and it might be a good
          idea to take into account the fact that some books have more than one
          author.

          --
          Toby A Inkster BSc (Hons) ARCS
          Fast withdrawal casino UK 2025 – Play now & cash out instantly! Discover the top sites for rapid, secure payouts with no delays.

          Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

          * = I'm getting there!

          Comment

          Working...