473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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_id($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

Apr 21 '07 #1
4 1498
ch***************@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_id($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.
js*******@attglobal.net
==================
Apr 22 '07 #2
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...@attglobal.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_id($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...@attglobal.net
==================

Apr 22 '07 #3
ch***************@gmail.com wrote:
On 22 apr, 04:43, Jerry Stuckle <jstuck...@attglobal.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_id($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...@attglobal.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::MakeTicket 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::PrintTicket 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.
js*******@attglobal.net
==================
Apr 22 '07 #4
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("Authors 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("Books 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($library['emma']);
$authors['JA']->give_credit($library['pp']);
$authors['JA']->give_credit($library['ss']);
$authors['JA']->give_credit(new Book('Love & Freindship'));

$authors['EB'] =new Author('Bronte', 'Emily');
$authors['EB']->give_credit($library['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_catalogue();
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
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Apr 23 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Wolfgang Schwanke | last post by:
Dear usenet, I'm having the following small problem. I've been ask to add some Quicktime panoramas to a website. The author of the panoramas has made two versions of each: One in MOV format,...
8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
8
by: Hardrock | last post by:
I encountered some difficulty in implementing dynamic loop nesting. I.e. the number of nesting in a for(...) loop is determined at run time. For example void f(int n) { For(i=0; i<=K; i++)...
0
by: Raffi B. | last post by:
I have a form named MainWindow in my project. Visual Studio C# Express automatically creates MainWindow.Designer.cs and MainWindow.resx as partial classes on MainWindow. I am trying to add a new C#...
4
by: kl.vanw | last post by:
I would like to count the nesting level in template classes. How can I make the following work? #include <assert.h> template <class T> class A { public: A() { // what goes here?
4
by: joey.powell | last post by:
I am writing a class that will do some binary file IO. The class will need to read a header from the binary file, and it will also need to read a varying number of records in the file. I currently...
0
by: Karch | last post by:
Does anyone know how I can tell Visual Studio 2005 to nest additional partial class files. For example: Default.aspx Default.aspx.cs (nests by default) Default.aspx.designer.cs (nests by...
6
by: stephen.cunliffe | last post by:
Hi, I'm looking for opinion/facts/arguments on the correct nesting of UL, OL, & LI elements. For example, this is what I want (unordered list): * Item 1 * Item 2 * Item 3
17
by: henry | last post by:
Folks Here's a skeleton, generic HTML page, call it "index.php". You'll see a bit of php code in the middle: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.