Jump to content

How to create an object that is related to a second object?


pandu

Recommended Posts

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.

 

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/>";

}

}

 

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.