silverglade Posted June 22, 2011 Share Posted June 22, 2011 Hi, I have some code I am a little confused about. If anyone can help me I'd appreciate it. Below is the code and I have commented my question within the code. Thanks. class DogTag { public $Words; } class Dog { public $Name; public $DogTag;// I don't know what this variable is for it looks like we never use it. //since we make the new DogTag object below from the class, what // do we need the $DogTag variable for please? public function bark() { print "Woof!\n"; } } $poppy = new Poodle; $poppy->Name = "Poppy"; $poppy->DogTag = new DogTag; $poppy->DogTag->Words ="My name is Poppy. If you find me please call 555-1234"; Link to comment https://forums.phpfreaks.com/topic/240052-objects-within-objects-question/ Share on other sites More sharing options...
gizmola Posted June 22, 2011 Share Posted June 22, 2011 The code is very simple: $poppy->DogTag = new DogTag; This is creating a "DogTag" object and assigning it to $poppy->DogTag. So clearly the DogTag variable in the Dog object IS being used, so store an associated DogTag object. Link to comment https://forums.phpfreaks.com/topic/240052-objects-within-objects-question/#findComment-1233092 Share on other sites More sharing options...
silverglade Posted June 22, 2011 Author Share Posted June 22, 2011 Oops. that was simple. I wasn't used to seeing a variable with -> on it. I was used to seeing this. $newObject = new Object; Thank you for explaining it. Link to comment https://forums.phpfreaks.com/topic/240052-objects-within-objects-question/#findComment-1233094 Share on other sites More sharing options...
gizmola Posted June 22, 2011 Share Posted June 22, 2011 The -> is used to access the variables and methods of an object: echo $obj->variable; echo $obj->method(); // assumes that function method() returns something. Link to comment https://forums.phpfreaks.com/topic/240052-objects-within-objects-question/#findComment-1233096 Share on other sites More sharing options...
silverglade Posted June 22, 2011 Author Share Posted June 22, 2011 Ok thank you :) Link to comment https://forums.phpfreaks.com/topic/240052-objects-within-objects-question/#findComment-1233098 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.