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"; Quote 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. Quote 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. Quote 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. Quote 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 :) Quote Link to comment https://forums.phpfreaks.com/topic/240052-objects-within-objects-question/#findComment-1233098 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.