TomTees Posted October 1, 2010 Share Posted October 1, 2010 I have a Soup object in a Bowl object in a Microwave object. The Microwave object has the methods: receiveItem(Bowl $b) cookItem(???) I want the Microwave to be able to set the Soup->temperature but I'm not how to do that? Make sense? TomTees Quote Link to comment https://forums.phpfreaks.com/topic/214871-set-property-in-object-of-object-of-object/ Share on other sites More sharing options...
trq Posted October 1, 2010 Share Posted October 1, 2010 We are going to need more details I think. Can we see some code? Putting it simply though, the Soup object would need a method that sets its temperature. Quote Link to comment https://forums.phpfreaks.com/topic/214871-set-property-in-object-of-object-of-object/#findComment-1117790 Share on other sites More sharing options...
TomTees Posted October 1, 2010 Author Share Posted October 1, 2010 We are going to need more details I think. Can we see some code? Putting it simply though, the Soup object would need a method that sets its temperature. I figured it out. But do you wanna see my silly soup app?! I created it to *try* and get a better understanding of how I think OOP works. (I can ZIP it up and post it if anyone wants to critique it?! Although no one wanted to open my ZIP earlier?!) TomTees Quote Link to comment https://forums.phpfreaks.com/topic/214871-set-property-in-object-of-object-of-object/#findComment-1117792 Share on other sites More sharing options...
fortnox007 Posted October 1, 2010 Share Posted October 1, 2010 I do want to see your silly soup app : ) I bet more people want to know the answer Quote Link to comment https://forums.phpfreaks.com/topic/214871-set-property-in-object-of-object-of-object/#findComment-1117796 Share on other sites More sharing options...
TomTees Posted October 1, 2010 Author Share Posted October 1, 2010 I do want to see your silly soup app : ) I bet more people want to know the answer See attached... TomTees [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/214871-set-property-in-object-of-object-of-object/#findComment-1117808 Share on other sites More sharing options...
fortnox007 Posted October 1, 2010 Share Posted October 1, 2010 thanks for sharing! Quote Link to comment https://forums.phpfreaks.com/topic/214871-set-property-in-object-of-object-of-object/#findComment-1117825 Share on other sites More sharing options...
trq Posted October 1, 2010 Share Posted October 1, 2010 Iv'e only had a really quick look (I'm at work), but you seem to be on the right track. One thing I would do however is make a base 'Food' class. Bowls can hold more than just soup so there is no point locking a bowl into only being able to hold soup. From there you would make your Soup class extend food. Because a 'type' might not belong to all foods, you should keep that within Soup, just put the generic stuff in 'Food'. You might have a bowl of 'porridge' for instance (I know, you can get different flavours/types of porridge these day, I'm just trying to point out that you put the more generic stuff in the food class and extend it with the more specific stuff). So your code might look more like.... class Food { private $type; private $temperature; public function __construct($t=null){ $this->type = $t; } public function setTemperature($temp=null){ $this->temperature = $temp; } public function getTemperature(){ return $this->temperature; } } class Soup extends Food { public function getType() { return $this->type; } } While the FillBowl() method now excepts Food. public function fillBowl(Food $s){ $this->contents = $s; } You should be able to do much the same with the Bowl object given that, you can also put plates, lunch boxes, cups etc etc in a microwave. Doing this (as I'm sure you understand) suddenly makes you classes allot more flexible. Quote Link to comment https://forums.phpfreaks.com/topic/214871-set-property-in-object-of-object-of-object/#findComment-1117827 Share on other sites More sharing options...
chintansshah Posted October 1, 2010 Share Posted October 1, 2010 Using inheritance you can avoid such situation, or you can make your method public access... Quote Link to comment https://forums.phpfreaks.com/topic/214871-set-property-in-object-of-object-of-object/#findComment-1117828 Share on other sites More sharing options...
trq Posted October 1, 2010 Share Posted October 1, 2010 Oh, one more quick thing. I think your naming your methods too specifically. eg; There is no point having a methods called getSoupTemperature() within a soup object, getTemperature() should suffice. Quote Link to comment https://forums.phpfreaks.com/topic/214871-set-property-in-object-of-object-of-object/#findComment-1117829 Share on other sites More sharing options...
ignace Posted October 1, 2010 Share Posted October 1, 2010 @thorpe $food = new Food(); $food->setTemperature(25); Why do people still use micro-waves? Quote Link to comment https://forums.phpfreaks.com/topic/214871-set-property-in-object-of-object-of-object/#findComment-1117847 Share on other sites More sharing options...
trq Posted October 1, 2010 Share Posted October 1, 2010 @thorpe $food = new Food(); $food->setTemperature(25); Why do people still use micro-waves? You have a point. Like I said, I was in a rush Quote Link to comment https://forums.phpfreaks.com/topic/214871-set-property-in-object-of-object-of-object/#findComment-1117848 Share on other sites More sharing options...
ignace Posted October 1, 2010 Share Posted October 1, 2010 Here's my take on the subject which I believe has strong relations with the Observer pattern. interface Cookable { function changeTemperature(MicroWave $mw); } class BowlOfSoup implements Cookable { private $temperature = 0.0; // darn cold soup function changeTemperature(MicroWave $mw) { $this->temperature = $mw->getTemperature() * 1.267; } } class MicroWave { private $item = null; private $config = null; private $temperature = 0.0; function __construct() { $this->config = new MicroWaveConfig($this); } function insert(Cookable $item) { $this->item = $item; return $this->config; } function start() { for($i = 0; $i < $this->config->getCookTime(); ++$i) { $this->temperature = $i * 1.027; // on each second the temperature increases 2.7% $this->notifyTemperatureChanged(); } } function getCookedItem() { return $this->item; } function getTemperature() { return $this->temperature; } private function notifyTemperatureChanged() { $this->item->changeTemperature($this); } } class MicroWaveConfig { private $microWave = null; private $cookTime = 0; private $waveIntensity = 0.1; function __construct(MicroWave $mw) { $this->microWave = $mw; } function getCookTime() { return $this->microWave; } function setCookTime($cookTime) { $this->cookTime = $cookTime; } function getWaveIntensity() { return $this->waveIntensity; } function setWaveIntensity($intense) { $this->waveIntensity = $intense; } function start() { $this->microWave->start(); } } $microWave = new MicroWave(); // spawn to World *MAGIC* $microWave->insert(new BowlOfSoup('Exquisite')) ->setCookTime(180)->setWaveIntensity(0.6)->start(); $soup = $microWave->getCookedItem(); Quote Link to comment https://forums.phpfreaks.com/topic/214871-set-property-in-object-of-object-of-object/#findComment-1117897 Share on other sites More sharing options...
TomTees Posted October 1, 2010 Author Share Posted October 1, 2010 Thorpe, Great feedback!! I totally follow what you said, and agree, however realize this was just a simple, silly example I put together to get a better handle of how to work with Classes, Objects, and OOP. (Technically, this was my first-ever attempt at a mini OOP app. And short of a few tips from PHP Freaks, I basically did this entirely by myself yesterday, so I am feeling better about me surviving this whole OOP thing!) If I was going to expand this app, I would totally do what you recommended. And since this is a "cute" and easy to relate to example, maybe I will expand it in my free time, as it would make a great teaching device!! Thanks, TomTees Quote Link to comment https://forums.phpfreaks.com/topic/214871-set-property-in-object-of-object-of-object/#findComment-1117990 Share on other sites More sharing options...
TomTees Posted October 1, 2010 Author Share Posted October 1, 2010 Oh, one more quick thing. I think your naming your methods too specifically. eg; There is no point having a methods called getSoupTemperature() within a soup object, getTemperature() should suffice. I agree 110%. (I just did that to help me keep things straight as I was starting to get really mixed up with all of those objects late last night!!) Thanks, TomTees Quote Link to comment https://forums.phpfreaks.com/topic/214871-set-property-in-object-of-object-of-object/#findComment-1117992 Share on other sites More sharing options...
TomTees Posted October 1, 2010 Author Share Posted October 1, 2010 Hang with me, Ignace, I'm going to get this eventually!!! See my comments/questions in RED below... Here's my take on the subject which I believe has strong relations with the Observer pattern. interface Cookable { function changeTemperature(MicroWave $mw); } class BowlOfSoup implements Cookable { private $temperature = 0.0; // darn cold soup function changeTemperature(MicroWave $mw) { $this->temperature = $mw->getTemperature() * 1.267; } } class MicroWave { private $item = null; private $config = null; private $temperature = 0.0; function __construct() { $this->config = new MicroWaveConfig($this); } Not sure I follow this?! It looks recursive?! You have two $this's... function insert(Cookable $item) { $this->item = $item; return $this->config; } function start() { for($i = 0; $i < $this->config->getCookTime(); ++$i) { $this->temperature = $i * 1.027; // on each second the temperature increases 2.7% $this->notifyTemperatureChanged(); } } function getCookedItem() { return $this->item; } function getTemperature() { return $this->temperature; } private function notifyTemperatureChanged() { $this->item->changeTemperature($this); } } class MicroWaveConfig { private $microWave = null; private $cookTime = 0; private $waveIntensity = 0.1; function __construct(MicroWave $mw) { $this->microWave = $mw; } function getCookTime() { return $this->microWave; } function setCookTime($cookTime) { $this->cookTime = $cookTime; } function getWaveIntensity() { return $this->waveIntensity; } function setWaveIntensity($intense) { $this->waveIntensity = $intense; } function start() { $this->microWave->start(); } } $microWave = new MicroWave(); // spawn to World *MAGIC* $microWave->insert(new BowlOfSoup('Exquisite')) ->setCookTime(180)->setWaveIntensity(0.6)->start(); I think "BowlOfSoup" needs a "name" property if you are passing it 'Exquisite' in the construction phase, right? I think you are missing a semi-colon after ('Exquisite')) I am totally lost how you are "daisy-chaining together these methods above?? ->setCookTime(180)->setWaveIntensity(0.6)->start(); $soup = $microWave->getCookedItem(); Not to be too anal-retentive, but technically this should be called something like $myBowl and not $soup since you are really getting a heated up "Bowl of Soup", right? [/code] Thanks for your help!!! TomTees Quote Link to comment https://forums.phpfreaks.com/topic/214871-set-property-in-object-of-object-of-object/#findComment-1118009 Share on other sites More sharing options...
trq Posted October 1, 2010 Share Posted October 1, 2010 I think ignace might have simply got a little rushed at the end of his example. The design itself is allot better than mine and mimics the real world allot closer. I always have trouble finding the pattern top fit the problem, but I was also trying to keep it pretty simple. I think "BowlOfSoup" needs a "name" property if you are passing it 'Exquisite' in the construction phase, right? Your right, this arument being passed to the __construct() isn't being used in this example. I think you are missing a semi-colon after ('Exquisite')) Not really, he is chaining the methods together. See my next response though.... I am totally lost how you are "daisy-chaining together these methods above?? ->setCookTime(180)->setWaveIntensity(0.6)->start(); $soup = $microWave->getCookedItem(); You can chain methods together like that. However, each of the methods need to actually return $this. In this case they don't so it wouldn't work, but the idea is good. Quote Link to comment https://forums.phpfreaks.com/topic/214871-set-property-in-object-of-object-of-object/#findComment-1118175 Share on other sites More sharing options...
TomTees Posted October 2, 2010 Author Share Posted October 2, 2010 I think ignace might have simply got a little rushed at the end of his example. The design itself is allot better than mine and mimics the real world allot closer. I always have trouble finding the pattern top fit the problem, but I was also trying to keep it pretty simple. Yeah, the more read his and other people's examples, the more things click a little bit. I think "BowlOfSoup" needs a "name" property if you are passing it 'Exquisite' in the construction phase, right? Your right, this arument being passed to the __construct() isn't being used in this example. Okay, good. I think you are missing a semi-colon after ('Exquisite')) Not really, he is chaining the methods together. See my next response though.... I am totally lost how you are "daisy-chaining together these methods above?? ->setCookTime(180)->setWaveIntensity(0.6)->start(); $soup = $microWave->getCookedItem(); You can chain methods together like that. However, each of the methods need to actually return $this. In this case they don't so it wouldn't work, but the idea is good. So he was trying to say this? $microWave->insert(new BowlOfSoup('Exquisite')) $microWave->setCookTime(180) $microWave->setWaveIntensity(0.6) $microWave->start(); TomTees Quote Link to comment https://forums.phpfreaks.com/topic/214871-set-property-in-object-of-object-of-object/#findComment-1118192 Share on other sites More sharing options...
trq Posted October 2, 2010 Share Posted October 2, 2010 So he was trying to say this? Quote $microWave->insert(new BowlOfSoup('Exquisite')) $microWave->setCookTime(180) $microWave->setWaveIntensity(0.6) $microWave->start(); TomTees If we fix ignaces example a little. class MicroWaveConfig { private $microWave = null; private $cookTime = 0; private $waveIntensity = 0.1; function __construct(MicroWave $mw) { $this->microWave = $mw; } function getCookTime() { return $this->microWave; } function setCookTime($cookTime) { $this->cookTime = $cookTime; return $this; } function getWaveIntensity() { return $this->waveIntensity; } function setWaveIntensity($intense) { $this->waveIntensity = $intense; return $this; } function start() { $this->microWave->start(); } } You can now chain these methods together.... $microWave = new MicroWave(); // spawn to World *MAGIC* $microWave->insert(new BowlOfSoup()) ->setCookTime(180)->setWaveIntensity(0.6)->start(); because each of the methods returns the MicroWaveConfig object. Quote Link to comment https://forums.phpfreaks.com/topic/214871-set-property-in-object-of-object-of-object/#findComment-1118194 Share on other sites More sharing options...
trq Posted October 2, 2010 Share Posted October 2, 2010 I should mention new lines don't matter, so... $microWave = new MicroWave(); // spawn to World *MAGIC* $microWave->insert(new BowlOfSoup()) ->setCookTime(180)->setWaveIntensity(0.6)->start(); could be.... $microWave = new MicroWave(); // spawn to World *MAGIC* $microWave ->insert(new BowlOfSoup()) ->setCookTime(180) ->setWaveIntensity(0.6) ->start(); or... $microWave = new MicroWave(); // spawn to World *MAGIC* $microWave->insert(new BowlOfSoup())->setCookTime(180)->setWaveIntensity(0.6)->start(); You can also so still call them the more traditional way.... $microWave->insert(new BowlOfSoup('Exquisite')) $microWave->setCookTime(180) $microWave->setWaveIntensity(0.6) $microWave->start(); but as you can see, chaining is a simpler (shorter) syntax. This stuff is just candy though, but allot of frameworks use it. Quote Link to comment https://forums.phpfreaks.com/topic/214871-set-property-in-object-of-object-of-object/#findComment-1118195 Share on other sites More sharing options...
TomTees Posted October 2, 2010 Author Share Posted October 2, 2010 class MicroWaveConfig { private $microWave = null; private $cookTime = 0; private $waveIntensity = 0.1; function __construct(MicroWave $mw) { $this->microWave = $mw; } function getCookTime() { return $this->microWave; } function setCookTime($cookTime) { $this->cookTime = $cookTime; return $this; } function getWaveIntensity() { return $this->waveIntensity; } function setWaveIntensity($intense) { $this->waveIntensity = $intense; return $this; } function start() { $this->microWave->start(); }} You can now chain these methods together because each of the methods returns the MicroWaveConfig object. So return $this; returns the object MicroWaveConfig ?? TomTees P.S. Thorpe, so it looks like you're pretty handy with OOP yourself?! Quote Link to comment https://forums.phpfreaks.com/topic/214871-set-property-in-object-of-object-of-object/#findComment-1118219 Share on other sites More sharing options...
trq Posted October 2, 2010 Share Posted October 2, 2010 P.S. Thorpe, so it looks like you're pretty handy with OOP yourself?! Not in particularly, theres guys around here that are allot better practiced than I. Ignace would be one of them. I do use it though. I've written my own framework, and have worked with a few of the already existing ones. It is something that takes practice though, recognizing what design pattern will solve the problem at hand. Quote Link to comment https://forums.phpfreaks.com/topic/214871-set-property-in-object-of-object-of-object/#findComment-1118221 Share on other sites More sharing options...
ignace Posted October 2, 2010 Share Posted October 2, 2010 When I posted my example I was actually seeking for someone to concur or argument against my design - as there are quite some intelligent people around here (thorpe, CV, Daniel, Mchl, PFMaBisMad, ..) - not out to impress anyone. OO design is difficult, even more so if you have a low domain knowledge (like I have on micro-waves ). For example I doubt you would express the created energy in a micro-wave as temperature (the object inside heats up due to the generated energy) or that the wave intensity would be able to correlate to a simple float but being an example I thought "/care" Quote Link to comment https://forums.phpfreaks.com/topic/214871-set-property-in-object-of-object-of-object/#findComment-1118242 Share on other sites More sharing options...
TomTees Posted October 2, 2010 Author Share Posted October 2, 2010 OO design is difficult, That's why I'm asking for your (and anyone else's) help!!!!! even more so if you have a low domain knowledge (like I have on micro-waves ). Ha ha. For example I doubt you would express the created energy in a micro-wave as temperature (the object inside heats up due to the generated energy) or that the wave intensity would be able to correlate to a simple float but being an example I thought "/care" I just chose this example because everyone can understand Soup, Bowl, Microwave. And it was something easy for me to put my head around as far as Object relationships, properties, and methods. Now on to a "real" example in the next few days! Thanks for all of your help, and I'll be back to haunt you again soon! TomTees Quote Link to comment https://forums.phpfreaks.com/topic/214871-set-property-in-object-of-object-of-object/#findComment-1118278 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.