473,379 Members | 1,245 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,379 software developers and data experts.

No Nested Classes in php? How would I set up the following...?

A simplified example of what I am looking for comes from a restaurant.
  • The Restaurant has 30 tables (Parent Class).
  • Each table has a server and location (Parent Properties).
  • At each table is an array of patrons (Child Class).
  • Each patron has name and alergies (Child Prop's).

I thought to set it up like this:
Expand|Select|Wrap|Line Numbers
  1. class patron {
  2.   public Name;
  3.   public Allergies;
  4. }
  5. class table {
  6.   public Server;
  7.   public Location;
  8.   public patrons = array();
  9. }
  10. $Table = array();
  11. $Table[0] = new table();
  12. $Table[0]->patrons[0]-> new patron();
  13.  
Any examples of a workaround for this would be great. Thanks in advance.
Apr 3 '11 #1

✓ answered by Dormilich

your general idea is not far from sensible, you just need to find a good way to pass the objects around.

first we start developing a class for the patrons (I’ll call them guests, to make it more obvious). you have to ask, what functionality a Guest object needs. well, every guest has a name, and (optionally allergies). these are your initial properties. next is functionality, what do you need to know about a guest—his properties. you need to make him tell you that. these will be methods.

for design reasons, you define the public methods beforehand.
Expand|Select|Wrap|Line Numbers
  1. // common specifics about a person
  2. interface iPerson
  3. {
  4.     public function getName();
  5. }
  6. // restaurant specific info (allergies)
  7. interface iHealth
  8. {
  9.     public function getAllergies();
  10.     public function hasAllergy($name);
  11.     public function addAllergy($name);
  12.     public function healAllergy($name);
  13. }
this is what a guest can tell us. now we code that into the Guest class:
Expand|Select|Wrap|Line Numbers
  1. class Guest implements iPerson, iHealth
  2. {
  3.     private 
  4.           $name    = '' // a name once given hardly changes
  5.         , $allergy = array() // no allergies by default
  6.     ;
  7.     // assign the name
  8.     public function __construct($name)
  9.     {
  10.         $this->name = name;
  11.     }
  12.  
  13.     public function getAllergies()
  14.     {
  15.         return $this->allergy;
  16.     }
  17.  
  18.     public function addAllergy($name)
  19.     {
  20.         $this->allergy[] = $name;
  21.     }
  22.  
  23.     public function healAllergy($name)
  24.     {
  25.         // test your cleverness here ...
  26.     }
  27.  
  28.     public function hasAllergy($name)
  29.     {
  30.         // test your cleverness here ...
  31.     }
  32. }
now we have a class, that contains all we need to know about the guest (for now at least).

next is the Table class. the process is the same, you need to ask what a table is/has and what it can do. again, interfaces help being consistent in the behaviour. (I leave writing that part to you)

the interesting part is adding a Guest to a Table. good practice is to use a so-called setter method, i.e.
Expand|Select|Wrap|Line Numbers
  1. public function addGuest($name, iHealth $guest)
  2. {
  3.     $this->guest[$name] = $guest;
  4. }
the iHealth interface here ensures, that you can "ask" the guest about his allergies independently of the class used for object construction.

as example the verification for a dish:
Expand|Select|Wrap|Line Numbers
  1. public function orderDish(iAllergy $dish)
  2. {
  3.     // if a guest has an allergy, deny order
  4.     // using a specific guest as simplified example
  5.     $guest = $this->guest['Homer'];
  6.     $match = array_intersect($guest->getAllergies(), $dish->getAllergenes());
  7.     if (count($match) > 0)
  8.     {
  9.         throw new Exception("Homer can’t eat " . $dish->getName()); 
  10.     }
  11.     // private order method
  12.     $this->placeOrder($dish);
  13. }
note: allergene – triggers an allergic reaction

3 2797
Dormilich
8,658 Expert Mod 8TB
just for clarification, what is a patron of a table?
Apr 4 '11 #2
A Patron in this case is someone who is hungry and is going to eat. If Uncle Bob, Aunt Sue and Cousin Larry go to dinner, that's 3 "patrons" at one table.
Apr 6 '11 #3
Dormilich
8,658 Expert Mod 8TB
your general idea is not far from sensible, you just need to find a good way to pass the objects around.

first we start developing a class for the patrons (I’ll call them guests, to make it more obvious). you have to ask, what functionality a Guest object needs. well, every guest has a name, and (optionally allergies). these are your initial properties. next is functionality, what do you need to know about a guest—his properties. you need to make him tell you that. these will be methods.

for design reasons, you define the public methods beforehand.
Expand|Select|Wrap|Line Numbers
  1. // common specifics about a person
  2. interface iPerson
  3. {
  4.     public function getName();
  5. }
  6. // restaurant specific info (allergies)
  7. interface iHealth
  8. {
  9.     public function getAllergies();
  10.     public function hasAllergy($name);
  11.     public function addAllergy($name);
  12.     public function healAllergy($name);
  13. }
this is what a guest can tell us. now we code that into the Guest class:
Expand|Select|Wrap|Line Numbers
  1. class Guest implements iPerson, iHealth
  2. {
  3.     private 
  4.           $name    = '' // a name once given hardly changes
  5.         , $allergy = array() // no allergies by default
  6.     ;
  7.     // assign the name
  8.     public function __construct($name)
  9.     {
  10.         $this->name = name;
  11.     }
  12.  
  13.     public function getAllergies()
  14.     {
  15.         return $this->allergy;
  16.     }
  17.  
  18.     public function addAllergy($name)
  19.     {
  20.         $this->allergy[] = $name;
  21.     }
  22.  
  23.     public function healAllergy($name)
  24.     {
  25.         // test your cleverness here ...
  26.     }
  27.  
  28.     public function hasAllergy($name)
  29.     {
  30.         // test your cleverness here ...
  31.     }
  32. }
now we have a class, that contains all we need to know about the guest (for now at least).

next is the Table class. the process is the same, you need to ask what a table is/has and what it can do. again, interfaces help being consistent in the behaviour. (I leave writing that part to you)

the interesting part is adding a Guest to a Table. good practice is to use a so-called setter method, i.e.
Expand|Select|Wrap|Line Numbers
  1. public function addGuest($name, iHealth $guest)
  2. {
  3.     $this->guest[$name] = $guest;
  4. }
the iHealth interface here ensures, that you can "ask" the guest about his allergies independently of the class used for object construction.

as example the verification for a dish:
Expand|Select|Wrap|Line Numbers
  1. public function orderDish(iAllergy $dish)
  2. {
  3.     // if a guest has an allergy, deny order
  4.     // using a specific guest as simplified example
  5.     $guest = $this->guest['Homer'];
  6.     $match = array_intersect($guest->getAllergies(), $dish->getAllergenes());
  7.     if (count($match) > 0)
  8.     {
  9.         throw new Exception("Homer can’t eat " . $dish->getName()); 
  10.     }
  11.     // private order method
  12.     $this->placeOrder($dish);
  13. }
note: allergene – triggers an allergic reaction
Apr 6 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

26
by: Joshua Beall | last post by:
Hi All, I remember reading that both nested classes and namespaces would be available in PHP5. I know that namespaces got canceled (much sadness...), however, I *thought* that nested classes...
10
by: Paul Morrow | last post by:
I'm hoping that someone can explain why I get the following exception. When I execute the code... ###################################### class Parent(object): class Foo(object): baz = 'hello...
3
by: Erik Bongers | last post by:
Hi, Nested classes only seem to be able to access static members of the surrounding class : class SurroundingClass { public: class InnerClass { public:
7
by: Alfonso Morra | last post by:
I have a class that contains a nested class. The outer class is called outer, and the nested class is called inner. When I try to compile the following code, I get a number of errors. It is not...
6
by: B0nj | last post by:
I've got a class in which I want to implement a property that operates like an indexer, for the various colors associated with the class. For instance, I want to be able to do 'set' operations...
8
by: Etienne Boucher | last post by:
Nested classes are usualy objects made to only live in their parent object instance. In other words... public class Outter { public class Inner { } }
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
2
by: Bob Day | last post by:
Using VS2003, VB.NET, MSDE... I am looking at a demo program that, to my surprise, has nested classes, such as the example below. I guess it surprised me becuase you cannot have nested subs,...
1
by: Raju Joseph | last post by:
Hi All, We are in the process of developing an N-Tier app using VB.NET. We are extensively using classes (entity objects) in our design. Further, most of the times, we do have to specify nested...
5
by: Jake K | last post by:
What purpose does nesting a class inside another class typically server? Are there conditions where this would be beneficial? Thanks a lot.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.