pandu Posted June 21, 2009 Share Posted June 21, 2009 Hello, I am writing some bullshit code for practice purposes and have a question. I have a class called person() with properties such as phone, name, etc. Now I have created a class called wife() which extends person. The way I want it to work, is that the wife() class will only 1. create a wife object for a person class that exists. 2. if the person exists, the wife object created will only be related to a specific person object. Eg. Mary will only be the wife of Tom. If I try to run wife(Tom, Jane), then the code should throw an error like "Tom is already married to Mary". But I am not sure how to define the wife class to create exclusive relationships between objects. Or maybe I should focus on simpler examples. Here is the code for the wife() class, which just creates a wife and does not even check whose wife it is or if they can have a wife: class wife extends person { function __construct($wifename) { $this->set_name($wifename); echo $wifename." is the wife <br/>"; } function cook($meal) { echo $this->name." is cooking ".$meal."<br/>".ucfirst($meal)." is ready! <br/>"; } function shopping() { echo $this->name." is happy because she is shopping"; } } TIA. Link to comment https://forums.phpfreaks.com/topic/163126-how-to-create-an-object-that-is-related-to-a-second-object/ Share on other sites More sharing options...
Ken2k7 Posted June 21, 2009 Share Posted June 21, 2009 Well, you need to set some way of storing who gets married to whom. I suggest storing them in an array such as - $array['wife'] = person('husband'). Link to comment https://forums.phpfreaks.com/topic/163126-how-to-create-an-object-that-is-related-to-a-second-object/#findComment-860659 Share on other sites More sharing options...
pandu Posted June 21, 2009 Author Share Posted June 21, 2009 Thanks for the idea Ken2K7. Based on your suggestion, I wrote the following code. I have not tested it, because I dont know how to pass an object as input to a class. However, I would appreciate any feedback: var marriage_registry = array(); function __construct($wife, $person) { foreach ($marriage_registry as $key => $value) { if ($key == $person) { // check to see if $person is in the marriage_registry $single_status = false; if ($value == $wifename) { // check to see if $person is already married to $wife echo "$wife is already married to $person"; else "$person is married to someone else"; } } if ($single_status == true) { $this->set_name($wifename); //create the wife marriage_db[this->get_name($person)] = $wife; //update the marriage_registry echo $wifename." is the wife of ".$person <br/>"; } } Link to comment https://forums.phpfreaks.com/topic/163126-how-to-create-an-object-that-is-related-to-a-second-object/#findComment-860685 Share on other sites More sharing options...
Ken2k7 Posted June 21, 2009 Share Posted June 21, 2009 Well marriage_registry should be a singleton array. Having it in a class is pointless unless you make it static. Probably best to make a marriage_registry class though. Link to comment https://forums.phpfreaks.com/topic/163126-how-to-create-an-object-that-is-related-to-a-second-object/#findComment-860689 Share on other sites More sharing options...
pandu Posted June 21, 2009 Author Share Posted June 21, 2009 Hey Ken2K, I took your suggestion and tried to setup an object called marriage_registry. My plan was to setup an associative array within this object to store marriage data. However, it seems that the data would be lost every time the marriage_registry object was created and closed. So my question is, how do you store data in an object so that the data persists thru sessions and run sequences? I am guessing that the only way to do so is to store the object data into a database. But wanted to check in and get your opinion. Thanks A LOT for your input. Link to comment https://forums.phpfreaks.com/topic/163126-how-to-create-an-object-that-is-related-to-a-second-object/#findComment-860888 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.